c语言文件的简单实例和说明

1、 从键盘输入到文本文件(fgetc和fputc的使用)

#include"stdio.h"

main()

{

FILE *fp; //定义文件指针

char ch,filename[100];  //filename为输入的文件名

printf("please input the filename:");

gets(filename);  //利用gets读取文件名

fp=fopen(filename,"w");  //以写的方式打开文件,如果没有就新建,

printf("please input somecharacter(end with!):");

while(ch!='!') //以!结束输入

{

       ch=getchar();  //一个一个读取字符

       fputc(ch,fp);   //将字符输入到文件中

}

fclose(fp); //关闭文件

}

说明:fgetc从文件中读取一个字符,如:char ch; ch=fgetc(fp);

fputc将字符输入到文件中,fputc(字符数据,文件指针)

 

2、 fgets和fputs的使用,

2.1从文件中读取20个字符(fgets)

#include"stdio.h"

main()

{

FILE *fp;

char str[21];

if((fp=fopen("test.txt","rt"))==NULL)

{

       printf("Cannotopen file strike any key exit!");

       exit(0);

}

fgets(str,20,fp);  //20个包括结束符'\0'

printf("%s\n",str);

fclose(fp);

}

2.2向文件fp中追加一个字符串

#include"stdio.h"

main()

{

FILE *fp;

char ch,str[20];

if((fp=fopen("test.txt","a+"))==NULL)

{

       printf("Cannotopen file strike any key exit!");

       exit(0);

}

printf("input a string:\n");

scanf("%s",str);//输入一串字符,碰到空格就结束

fputs(str,fp);

fclose(fp);

}

 

3、 数据块读写函数fread和fwrite

一般用于二进制文件的处理

fread(buffer,size,count,fp) 从fp所指向的当前位置开始,一次读入size个字节,重复count次,并放到buffer开始的内存中。

fwrite(buffer,size,count,fp) 从buffer开始一次输出size个字节,重复count次,并放到fp中

 

4、 格式化读写函数fprintf和fscanf

输入的对象是文件。fscanf(文件指针,格式化控制字符,输入项列表)

如:fscanf(fp,”%d,%d”,&a,&b);

fscanf(stdin,”%d,%d”,&a,&b);等价于scanf(”%d,%d”,&a,&b);

fprintf类似fscanf

4.1fprintf的使用

#include"stdio.h"

main()

{

FILE *fp;

char ch,str[20];int a,b;

fp=fopen("test.txt","w");

scanf("%d,%d",&a,&b);

fprintf(fp,"%d\n",a+b);

fclose(fp);

}

4.2fscanf的使用

#include"stdio.h"

main()

{

FILE *fp;

char ch,str[20];int a,b;

fp=fopen("test.txt","r");

fscanf(fp,"%d,%d",&a,&b);

fp=fopen("abc,txt","r+");

printf("%d\n",a+b);

fclose(fp);

}

5、  判断文件结束函数feof ,文件结束标志EOF,返回值为-1

5.1从文件中依次读取字符,并且逐个显示在屏幕上

#include"stdio.h"

main()

{

FILE *fp;

char ch;

fp=fopen("test.txt","r");

while(!feof(fp))   //feof(fp)返回的值为0

{

       ch=fgetc(fp);

       printf("%c",ch);

}

fclose(fp);

}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值