VC6.0把结果输出到文本文件中保存的方法是直接写入指定路径下的txt文件下。
以下程序将一个文件的内容附加到另一个文件中去。
设下面这个程序名为:linkfile.c
执行时可用:linkfile filename1 filename2形式的命令行运行,将文件filename2的内容附加在文件filename1之后。
#include
#define SIZE 512
main(int argc,char *argv[])
{ char buffer[SIZE];
FILE *fp1,*fp2;
if(argc!=3)
{ printf("Usage: linkfile filename1 filename2
exit(1);
}
if((fp1=fopen(argv[1],"a"))==NULL)
{ printf("The file %s can not be opened.\n",argv[1]);
exit(1);
}
if((fp2=fopen(argv[2],"r"))==NULL)
{ printf("The file %s can not be opened.\n",argv[2]);
exit(1);
}
while(fgets(buffer,SIZE,fp1)!=NULL)
printf("%s\n",buffer);
while(fgets(buffer,SIZE,fp2)!=NULL)
fputs(buffer,fp1);
fclose(fp1);
fclose(fp2);
if((fp1=fopen(argv[1],"r"))==NULL)
{ printf("The file %s can not be opened.\n",argv[1]);
exit(1);
}
while(fgets(buffer,SIZE,fp1)!=NULL)
printf("%s\n",buffer);
fclose(fp1);
}
main(){
FILE *fpt;
char a[]="hello world!";
fpt = fopen("wendangming.txt","w");//创建并打开文档,并得到文件句柄
fprintf(fpt,"%s",a); //通过文件句柄写入内容
fclose(fpt); //关闭文件
}