简体   繁体   English

如何在 Linux 上使用 c function 系统(命令)和转义字符“\3”

[英]How to use c function system(command) and escape character "\3" on Linux

I wrote a code using CodeBlocks on Windows.But I met problems when I Run the code on Linux, using its GCC compiler.我在 Windows 上使用 CodeBlocks 编写了一个代码。但是当我在 Linux 上使用其 GCC 编译器运行代码时遇到了问题。 while using the function system("title heartshape") and system("color XX") on Linux, it will show this error continuously:在 Linux 上使用 function system("title heartshape")system("color XX")时,它会不断显示此错误:

"sh: color: command not found" “sh:颜色:找不到命令”

Line 12 to 43 are the codes that can draw a heart shape, which is unused in this problem, and the for(;;) loop after that can change the color of the heart shape over time continuously.第 12 到 43 行是可以绘制心形的代码,在这个问题中没有用到,之后的for(;;)循环可以随着时间的推移不断地改变心形的颜色。

The source code as followed:源代码如下:

#include <math.h>
#include <stdlib.h>
#define I 20
#define R 340
int main(void)
{
    int i, j, e;
    int a;
    long time;
    system("title Heartshape");
/*codes that use to draw a heart, unhelpful to my question*/
    for (i = 1, a = I; i < I / 2; i++, a--)
    {
        for (j = (int)(I - sqrt(I * I - (a - i) * (a - i))); j > 0; j--)
            printf(" ");
        for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)
            printf("\3");
        for (j = (int)(2 * (I - sqrt(I * I - (a - i) * (a - i)))); j > 0; j--)
            printf(" ");
        for (e = 1; e <= 2 * sqrt(I * I - (a - i) * (a - i)); e++)
            printf("\3");
        printf("\n");
    }
    for (i = 1; i < 80; i++)
    {
        printf("\3");
    }
    printf("\n");
    for (i = 1; i <= R / 2; i++)
    {
        if (i % 2 || i % 3)
            continue;
        for (j = (int)(R - sqrt(R * R - i * i)); j > 0; j--)
            printf(" ");
        for (e = 1; e <= 2 * (sqrt(R * R - i * i) - (R - 2 * I)); e++)
            printf("\3");
        printf("\n");
    }
/*codes that use to change the color of the heart*/
    for (;;)
    {
        system("color a");
        for (time = 0; time < 99999999; time++);

        system("color b");
        for (time = 0; time < 99999999; time++);

        system("color c");
        for (time = 0; time < 99999999; time++);

        system("color d");
        for (time = 0; time < 99999999; time++);

        system("color e");
        for (time = 0; time < 99999999; time++);

        system("color f");
        for (time = 0; time < 99999999; time++);

        system("color 0");
        for (time = 0; time < 99999999; time++);

        system("color 1");
        for (time = 0; time < 99999999; time++);

        system("color 2");
        for (time = 0; time < 99999999; time++);

        system("color 3");
        for (time = 0; time < 99999999; time++);

        system("color 4");
        for (time = 0; time < 99999999; time++);

        system("color 5");
        for (time = 0; time < 99999999; time++);

        system("color 6");
        for (time = 0; time < 99999999; time++);

        system("color 7");
        for (time = 0; time < 99999999; time++);

        system("color 8");
        for (time = 0; time < 99999999; time++);

        system("color 9");
        for (time = 0; time < 99999999; time++);
    }
    return 0;
}

That's the whole source code of my project.这就是我项目的全部源代码。

The code can be run perfectly on Windows compiler, but keep showing the error sh: color: command not found on Linux.该代码可以在 Windows 编译器上完美运行,但一直显示错误sh: color: command not found on Linux。

In my limited experience with Linux for just a week, I can only assume that there're differences between Windows and Linux while using system() in c, and the escape character \3 won't show on Linux as well. In my limited experience with Linux for just a week, I can only assume that there're differences between Windows and Linux while using system() in c, and the escape character \3 won't show on Linux as well.

I've found the relevant question here , but the question seems remain unsolved, at least the answer doesn't help me.我在这里找到了相关问题,但问题似乎仍未解决,至少答案对我没有帮助。 Other answers seems too complicated to solve this question.其他答案似乎太复杂而无法解决这个问题。

so I wanted to know the differences in using the function system() ,as well as if there's good practice to change the color of the heart shape continuously on Linux?所以我想知道使用 function system()的区别,以及是否有在 Linux 上连续更改心形颜色的好习惯? Or are there's an alternative of it?还是有它的替代品?

Thanks in advance.提前致谢。

The problem is not with the function system() , even though it does different things under the hood.问题不在于 function system() ,即使它在引擎盖下做了不同的事情。 Of course, because Windows and Linux are different operation systems.当然,因为 Windows 和 Linux 是不同的操作系统。 But as a standard library function, it generally does the same for you on both systems: it lets the command processor of the OS execute an internal command or launch another program.但是作为一个标准库 function,它通常在两个系统上对您做同样的事情:它让操作系统的命令处理器执行内部命令或启动另一个程序。

The problem is with the programs (commands) you try to execute.问题在于您尝试执行的程序(命令)。 The commands "title" and "color" are unknown to my shell (bash on Arch Linux), but are known to Windows' shell ("cmd.exe").我的 shell(Arch Linux 上的 bash)不知道命令“title”和“color”,但 Windows 的 shell(“cmd.exe”)知道命令“title”和“color”。

A similar issue is your output of '\3' .类似的问题是您的 output of '\3' This special character is only shown with the specific font of your shell.此特殊字符仅与您的 shell 的特定字体一起显示。

You need to take another approach.你需要采取另一种方法。 There are several possible solutions, for example using "ncurses" as an OS independent library.有几种可能的解决方案,例如使用“ncurses”作为独立于操作系统的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM