调用 “system()” 函数后的所有行都没有运行。

huangapple go评论73阅读模式
英文:

None of the lines after calling the "system()" function is running

问题

After the line "command(system)", the next lines are not running. A new file "test1" is created but the next lines do not execute.

使用我的Linux Ubuntu终端,我正在尝试编写一个打开新文件的C程序。由于新文件已经创建,这意味着system()函数正在工作。但它不会停止执行。
如果我关闭终端,它会显示"进程正在运行"。我如何停止这个进程?在创建新文件后,我没有获得Linux终端的命令行。

英文:

After the line "command(system)", the next lines are not running. A new file "test1" is created but the next lines do no execute.

Using my linux Ubuntu terminal, I am trying to write a C program for opening a new file. Since the new file is created, it means that the system() function is working. But it does not stop executing.
If I close the terminal, it says "process is running". How do I stop the process? I do not get the linux terminal's line for command after a new file is created.


#include <stdio.h>
#include <stdlib.h>

int main() {
    char filename[50];

    printf("Enter the filename: ");
    scanf("%s", filename);

    // Create and open the file using the cat command
    char command[100];
    
    sprintf(command, "cat > %s", filename);
    system(command);
    printf("File '%s' created and opened.\n", filename);

    return 0;
}

In the directory, a new file named "test1" is created but in the terminal, as you can see, the code doesn't finish executing/doesn't stop.

答案1

得分: 2

> 在"command(system)"这行之后,接下来的行不会运行。一个新文件"test1"被创建,但接下来的行不会执行。

它们目前不会执行。这是因为通过system()运行的cat命令正在等待标准输入。system()函数在cat终止之前不会返回。在终端窗口中按Ctrl-D应该停止它的等待。

如果你只想创建一个空文件,那么将命令更改为重定向cat的标准输入将是一种方法:

    sprintf(command, "cat < /dev/null > %s", filename);
    system(command);

但如果这确实是你想做的全部,那么使用touch而不是cat会更符合惯用法:

    sprintf(command, "touch %s", filename);
    system(command);
英文:

> After the line "command(system)", the next lines are not running. A new file "test1" is created but the next lines do no execute.

They do not execute yet. That's because the cat command you are running via system() is waiting for input from its standard input. The system() function will not return until cat terminates. Typing Ctrl-D in the terminal window should make it stop waiting.

If all you want to do is create an empty file, then changing the command to redirect cat's standard input would be one way to go about that:

    sprintf(command, &quot;cat &lt; /dev/null &gt; %s&quot;, filename);
    system(command);

But if that's really all you want to do, then it would be more idiomatic to use touch instead of cat:

    sprintf(command, &quot;touch %s&quot;, filename);
    system(command);

答案2

得分: 2

这里的目标并不是完全清楚的。如果您只想创建一个空文件,可以使用常规的C标准库文件函数,而无需使用system()调用外壳程序。

类似以下的代码可能足够了:

FILE *handle = fopen(filename, "w");
fclose(handle);

如果文件已经存在,但您想要“清空”它:

truncate(filename, 0);

使用system()来运行一些具有文件重定向的命令极不可移植,可能没有意义。如果您想走这条路,只需创建一个bash(或Python、Ruby等)脚本。

英文:

It's not entirely clear what your goal is here. If you just want to create an empty file, you can do it using the regular C standard library file functions, without calling out to a shell with system().

Something like this might suffice:

FILE *handle = fopen(filename, &quot;w&quot;);
fclose(handle);

If the file already exists, but you want to "empty" it:

truncate(filename, 0);

Calling system() to run some commands with file redirection is extremely non-portable and probably pointless. If you want to go down that path, just make a bash (or Python, or Ruby...) script.

huangapple
  • 本文由 发表于 2023年6月9日 05:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定