我已經創建了一個文件,但是如何將輸出重定向到文件而不是終端?我嘗試使用>運算符,但不斷收到錯誤。任何幫助表示讚賞。如何將輸出重定向到C中的文件?
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp; //File handler
char cmd[] = "sort -n"; //sort -n sorts stdin in numerical order
char pmode[] = "w"; //write mode - we are to write to a pipe
fp = popen(cmd,pmode); //Open the pipe (run command cmd as a child process)
fprintf(fp, "3\n"); //WRITE data to pipe
fprintf(fp, "1\n");
fprintf(fp, "5\n");
fprintf(fp, "4\n");
fprintf(fp, "2\n");
pclose(fp);
return 0;
}
>是一個shell操作符,而不是C操作符。回到基礎並找出什麼是C以及什麼是shell腳本。 – Almo
同意@almo,避開sn。。 –
一個簡單的谷歌搜索將得到更快的答案,並且比在此處發佈帖子更省力。我只是不明白一些人。 – redFIVE