c语言中怎么能输出结果到文件中

来源:百度知道 编辑:UC知道 时间:2024/06/05 19:47:06
c语言中怎么能输出结果到文件中,谢谢了

随手写的一段小程序,可以参考一下

#include <stdio.h>
#include <process.h>

main(argc,argv)
int argc;
char *argv[];
{
FILE * fileHandle;
char a[4] = "abc";

if(argc != 2){
printf("%s fileName\n",argv[0]);
exit(-1);
}
if((fileHandle = fopen(argv[1], "wb")) == NULL){
printf("%s open error\n", argv[1]);
exit(-1);
}
fprintf(fileHandle, "%s\r\n", a);

fclose(fileHandle);
}

假设c程序编译链接后的可执行文件名为:aaa.exe
你在执行时输入:

aaa.exe > filename 输出到文件,覆盖原文件内容
aaa.exe >> filename 追加到文件中

还可以指定标准输出和标准错误输出,
aaa.exe 1> filename 只把标准输出写到文件
aaa.exe 2> filename 只把标准错误写到文件