LINUX C语言写一个读写文件的程序,读取的话,要把文件内容显示出来,写的话,就是写进文件里面咯

如题所述

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

int main()
{
FILE *pf = fopen("a.txt", "r");
if(pf == NULL) {
printf("open a.txt file failed!\n");
exit(0);
}

FILE *pf2 = fopen("b.txt", "w");
if(pf2 == NULL) {
printf("open b.txt file failed!\n");
fclose(pf);
exit(0);
}

char ch;
while(!feof(pf)) {
ch = fgetc(pf);
putchar(ch);
fputc(ch, pf2);
}

fclose(pf2);
fclose(pf);

return 0;
}


温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-08
FILE *fp;
char c1[512];
char c2[512];
int i=0;
if((fp=fopen("/路径",'r'))=NULL)
printf("cant open the file");
//读出文件内容并显示
while(!feof(fp))
{
c1[i++]=fgetc(fp);
}
printf("%s\n",c1);
fclose(fp);
//向文件内写内容
if((fp=fopen("/路径",'w'))=NULL)
printf("cant open the file");
printf("please input c2:\n");
scanf("%s",c2);
fprintf(fp,"%s",c2);
fclose(fp);追问

头文件之类的不用加吗?

相似回答