C 语言简单的文件顺序读写
原创
©著作权归作者所有:来自51CTO博客作者覺醒的原创作品,请联系作者获取转载授权,否则将追究法律责任
这里使用int fputc(int ch, FILE *fp)
和 int fgetc(FILE *fp)
进行文本文件的读取和复制。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int fbcopy(FILE *fpdest, FILE *fpsrc);
int main(void)
{
FILE *fpdest = NULL, *fpsrc = NULL;
int sum_num = fbcopy(fpdest, fpsrc);
if (sum_num != -1) {
printf("总字节数目为:%d\n", sum_num);
} else {
printf("操作失败!!%d\n", sum_num);
}
return 0;
}
int fbcopy(FILE *fpdest, FILE *fpsrc)
{
char get_char;
int sum_num = 0;
fpsrc = fopen("fuck.dat", "rb");
if (fpsrc == 0) {
printf("打开文件失败!!!!");
return -1;
}
fpdest = fopen("copy_success.txt", "wb");
if (fpdest == 0) {
printf("打开文件失败!!!!");
return -1;
}
while ((get_char = fgetc(fpsrc)) != EOF) {
fputc(get_char, fpdest);
sum_num++;
}
fclose(fpsrc);
fclose(fpdest);
return sum_num;
}