楼上正解,利用重定向符号">"到任意文件中。
例如:main.exe
int main()
{
printf("hello world\n");
printf("hello world1\n");
printf("hello world2\n");
printf("hello world3\n");
return 0;
}
此时若直接运行此程序,则会在Console上打印4行,而若加上main.exe > 1.txt,则会把输出全部打到文件中。
如LZ的程序,如用fprintf,则应该这样:
int main()
{
FILE *outfile;
outfile = fopen("output.txt","w");
fprintf(outfile, "\n%d: ", lineno);
fprintf(outfile, "hello world\n");
fprintf(outfile, "hello world1\n");
fprintf(outfile, "hello world2\n");
fprintf(outfile, "hello world3\n");
fclose(outfile);
return 0;
}
但既然用C++,则不如用fstream了。
温馨提示:答案为网友推荐,仅供参考