c语言从一个文件读取文字到另一个文件中

来源:互联网 发布:平面设计软件培训班 编辑:程序博客网 时间:2024/05/01 14:41
#include "stdio.h"#include "string.h"int main(){FILE *fileR, *fileW;char buf[1000];if((fileR = fopen("test.txt","r")) == NULL)printf("open error!\n");if((fileW = fopen("result.txt","w")) == NULL)printf("wirte opened error!\n");int i=0,size=0;char ch=fgetc(fileR);while(ch != EOF){buf[i++]=ch;fwrite(&ch, 1, 1, fileW);ch=fgetc(fileR);size++;}//用下面方法写出来的最后有点小bug/*while(!feof(fileR)){fread(&buf[i], 1, 1, fileR);fwrite(&buf[i], 1, 1, fileW);i++;size++;}*/for(i = 0; i < size; i++)   printf("%c", buf[i]);fclose(fileR);fclose(fileW);return 0;}

原创粉丝点击