前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【编程概念】---文件读写实例

【编程概念】---文件读写实例

作者头像
程序员互动联盟
发布2018-03-16 14:48:58
6660
发布2018-03-16 14:48:58
举报

实例1:读写字符文件,每次读取一个字符。

#include <stdio.h>

int main()

{

FILE *fpin ;

FILE *fpout;

char c;

fpout=fopen("c:\\dest.txt","wt");

if((fpin=fopen("c:\\test.txt","rt"))!=NULL)

{

c =fgetc(fpin);

while(c!=EOF)

{

fputc(c,fpout);

c=fgetc(fpin);

}

}

else

{

printf("file not exist!");

exit(1);

}

fclose(fpin);

fclose(fpout);

return 0;

}

实例2:读取字符文件,每次读入一个缓存里面。

#include <stdio.h>

#define MAXLEN 1024

int main()

{

FILE *fin;

FILE *fout=fopen("c:\\dest.txt","wt");

char buf[MAXLEN];

if((fin=fopen("c:\\test.txt","rt"))!=NULL)

{

char* c =fgets(buf,MAXLEN,fin);

while(c!=0)

{

fputs(buf,fout);

c=fgets(buf,MAXLEN,fin);

}

}

else

{

printf("file not exist!");

exit(1);

}

fclose(fin);

fclose(fout);

return 0;

}

实例3:读写字节文件,每次读入一个缓存里面。

#include <stdio.h>

#define MAXLEN 1024

int main()

{

FILE *fpin ;

FILE *fpout;

unsigned char buf[MAXLEN];

int c;

fpout=fopen("c:\\dest.jpg","wb");

if((fpin=fopen("c:\\test.jpg","rb"))!=NULL)

{

c = fread(buf,sizeof(unsigned char),MAXLEN,fpin);

while(c!=0)

{

fwrite(buf,sizeof(unsigned char),c,fpout);

c=fread(buf,sizeof(unsigned char),MAXLEN,fpin);

}

}

else

{

printf("file not exist!");

exit(1);

}

fclose(fpin);

fclose(fpout);

return 0;

}

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-11-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员互动联盟 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档