文件操作

字符读写函数  :fgetc和fputc

字符串读写函数:fgets和fputs

数据块读写函数:fread和fwrite

格式化读写函数:fscanf和fprinf

fopen fclose--将流式文件打开,关闭的函数
fseek--改变文件位置指针的位置,文件位置指向改变则读写数据时候位置改变
ftell--查看当前文件位置指针位置 
feof--查看是否走到文件末尾 feof(fp),用于测试fp所指向的文件的当前状态是否为“文件结束”。如果是,函数则返回的值是1(真),否则为0(假)。
rewind--文件位置指针指向文件首

注意:
1. 文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是:

  r(read): 读
  w(write): 写
  a(append): 追加
  t(text): 文本文件,可省略不写
  b(banary): 二进制文件
  +: 读和写

  2. “r”:凡用“r”打开一个文件时,该文件必须已经存在, 且只能从该文件读出。

  3.“w”: 用“w”打开的文件只能向该文件写入。 若打开的文件不存在,则以指定的文件名建立该文件,若打开的文件已经存在,则将该文件删去,重建一个新文件。

  4. “a ”:若要向一个已存在的文件追加新的信息,只能用“a ”方式打开文件。但此时该文件必须是存在的,否则将会出错。

  5. fopen:在打开一个文件时,如果出错,fopen将返回一个空指针值NULL。在程序中可以用这一信息来判别是否完成打开文件的工作,并作相应的处理。
1、
/*fgetc
文件内部的位置指针用以指示文件内部的当前读写位置,每读写一次,该指针均向后移动,它不需在程序中定义说明,而是由系统自动设置的。*/
#include <conio.h>
#include <stdlib.h>
#include<stdio.h>

int main()
{
 
 FILE *fp = NULL;
 
 char ch = 0;
 
 if( NULL ==  (fp = fopen("1.txt","r") )  )
  
    {
  
  printf("/nCannot open file strike any key exit!");
  
  getch();/*从控制台读取一个字符,但不显示在屏幕上,用getch();
    会等待你按下任意键,再继续执行下面的语句;需包含#include <conio.h>*/
  
  exit(1);//异常结束  
    } 
 
 
 ch = fgetc(fp);
 while(!feof(fp))
 { 
  putchar(ch);
  ch = fgetc(fp);
}

  
 fclose(fp);
 return 0; 
}

1注:在VC里,只有当文件位置指针(fp->_ptr)到了文件末尾,然后再发生读/写操作时,标志位(fp->_flag)才会被置为含有_IOEOF。然后再调用feof(),才会得到文件结束的信息。

因此,如果运行如下程序:
char ch;
while(!feof(fp))
{
ch = fgetc(fp);
putchar(ch);
}
会发现多输出了一个FF,原因就是在读完最后一个字符后,fp->flag仍然没有被置为_IOEOF,因而feof()仍然没有探测到文件结尾。直到再次调用fgetc()执行读操作,feof()才能探测到文件结尾。这样就多输出了一个-1(即FF)。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bingqing07/archive/2010/08/03/5785080.aspx

 


2、
//fputc 写字符到文件
#include <stdlib.h>
#include<stdio.h>
int main()
{
 
 FILE *fp = NULL; 
 char ch = 0; 
 if(NULL == (fp = fopen("2.txt","w+")))  
 {
  
  printf("Cannot open file press any key to exit!");
  
  getch();
  
  exit(1);
  
 }

 /*以下做数据采集*/
 
 printf("input  characters,press enter key  to exit:/n");
 
 ch = getchar();
 
 while (ch!='/n')
  
 {
  
  fputc(ch,fp);
  
  
  ch=getchar();
  
 }
 
 rewind(fp);//调整文件位置指针

 /*以下做输出显示*/
 
 ch = fgetc(fp);
 
 while(0 == feof(fp))
  
 {
  
  putchar(ch);
  
  ch=fgetc(fp);
  
 }
 
 printf("/n");
 
 fclose(fp);
 return 0;
 
}


3、
//fgets 读字符串从文件
#include <stdlib.h>
#include<stdio.h>

 

int main()

{
 
 FILE *fp;
 
 char str[10];
 
 if(NULL == (fp=fopen("1.txt","r")))
  
 {
  
  printf("/nCannot open file strike any key exit!");
  
  getch();
  
  exit(1);
  
 }
 
 fgets(str,10,fp);
 
 printf("%s/n",str);
 
 fclose(fp);
 return 0;
 
}

4、
//fputs 写字符串到文件
#include <stdlib.h>
#include<stdio.h>

int main()

{
 
 FILE *fp;
 
 char ch,st[20];
 
 if(NULL == (fp=fopen("1.txt","a+")))
  
 {
  
  printf("Cannot open file strike any key exit!");
  
  getch();
  
  exit(1);
  
 }
 
 printf("input a string:/n");
 
 scanf("%s",st);
 
 fputs(st,fp);
 
 rewind(fp);//rewind函数把文件内部位置指针移到文件首
 
 ch=fgetc(fp);
 
 while(0 == feof(fp))
  
 {
  
  putchar(ch);
  
  ch=fgetc(fp);
  
 }
 
 printf("/n");
 
 fclose(fp);
 return 0;
 
}

5、
/*fwrite fread常用的数据块读写
fread(buffer,size,count,fp);
fwrite(buffer,size,count,fp);
其中:

buffer  是一个指针,在fread函数中,它表示从文件中读出若干数据,存放到的缓冲区的首地址。在fwrite函数中,它表示存放往文件中写入的数据的首地址。

    size   表示每块数据块的字节数。

    count  表示要读写的数据块块数。

    fp     表示文件指针。
*/

 

struct student

{
 
 char name[10];
 
 int num;
 
 int age;
 
 char addr[15];
 
}boy1[2],boy2[2],*pb1,*pb2;

int main()

{
 
 FILE *fp = NULL;  
 unsigned int i = 0; 
 pb1 = boy1; 
 pb2 = boy2; 
 if(NULL == (fp=fopen("3.txt","w+")))  
 {
  
  printf("Cannot open file strike any key exit!");
  getch();
  exit(1);  
 } 
 printf("please input  data/n"); 
 for(i=0;i<2;++i,++pb1)
  
  scanf("%s%d%d%s",pb1->name,&pb1->num,&pb1->age,pb1->addr);//采集数据
 
 pb1 = boy1;//调整回首地址
 
 fwrite(pb1,sizeof(struct stu),2,fp);//写到文件
 
 rewind(fp);//文件位置指针调整
 
 fread(pb2,sizeof(struct stu),2,fp);//从文件中读取
 
 printf("/n name /t number /t age /t addr/n");
 
 for(i=0; i<2; ++i,++pb2)
  
  printf("%s/t%5d%7d%10s/n",pb2->name,pb2->num,pb2->age,pb2->addr);
 
 fclose(fp);
 return 0 ;

 
}

6、
//fscanf和fprintf格式化读写函数
fprintf格式化写到文件 fsanf 格式化从文件读
与上例比较,fscanf和fprintf函数每次只能读写一个结构数组元素
#include <stdlib.h>
#include<stdio.h>


struct student

{
 
 char name[10];
 
 int num;
 
 int age;
 
 char addr[15];
 
}boy1[2],boy2[2],*pb1,*pb2;

int main()

{
 
 FILE *fp = NULL;
 int i = 0; 
 pb1 = boy1; 
 pb2 = boy2; 
 if(NULL == (fp=fopen("4.txt","wb+")))  
 {  
  printf("Cannot open file strike any key exit!");  
  getch();  
  exit(1);  
 } 
 printf("please input data/n"); 
 for(i=0;i<2;++i,++pb1)  
  scanf("%s%d%d%s",pb1->name,&pb1->num,&pb1->age,pb1->addr); 

 pb1 = boy1;
 
 for(i=0;i<2;i++,pb1++)  
  fprintf(fp,"%s %d %d %s/n",pb1->name,pb1->num,pb1->age,pb1->  
  addr); //格式化写入

 rewind(fp); 

 for(i=0;i<2;++i,++pb2)  
  fscanf(fp,"%s %d %d %s/n",pb2->name,&pb2->num,&pb2->age,pb2->addr); 
 printf("/n name /t number      age      addr/n"); 
 pb2 = boy2; 
 for(i=0;i<2;i++,pb2++)  
  printf("%s/t%5d  %7d%10s/n",pb2->name,pb2->num, pb2->age,  
  pb2->addr); 
 fclose(fp);
 
}

 

7、
//文件的随机读写fseek
重定位流(数据流/文件)上的文件内部位置指针
SEEK_SET: 文件开头   
SEEK_CUR: 当前位置   
SEEK_END: 文件结尾
成功,返回0,否则返回其他值。

int main( void )
{
 FILE *fstream = NULL;
 char line[80];
 int  result = 0;
 
 fstream = fopen( "fseek.txt", "w+" );
 if( fstream == NULL )
  printf( "The file fseek.txt was not opened/n" );
 else
 {
  fprintf( fstream, "The fseek begins here: " //共23L个字符
   "This is the file 'fseek.txt'./n" );
  result = fseek( fstream, 23, SEEK_SET);//从文件开始位置开始右移23个字符位置
  if( result )
   printf( "Fseek failed" );
  else
  {
   printf( "File pointer is set to middle of first line./n" );
   fgets( line, 80, fstream );
   printf( "%s", line );
   
  }
  fclose( fstream );
 }
 return 0;
}

8、
//ftell
#include <stdio.h>

int main( void )
{
  
 FILE *fstream = NULL;
 long position = 0;
 char list[100] = {0,};
 if( (fstream = fopen( "1.txt", "rb" )) != NULL )
   {
      /* Move the pointer by reading data: */
      fread( list, sizeof( char ), 100, fstream );
      /* Get position after read: */
      position = ftell( fstream );
      printf( "Position after trying to read 100 bytes: %ld/n",
              position );
      fclose( fstream );
   }
}

9、二进制和文本文件的区别
仅仅是对'/n'的存储不同

int main( void )
{
   FILE *fstream = NULL; 
   if( (fstream = fopen( "1.txt", "w" )) != NULL )
   {
     
      fputs( "hello world!/n", fstream );
      fclose( fstream );
   }
   return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值