C/C++中文件操作的函数小节

一、打开与关闭

1.1fopen

FILE * fopen ( const char * filename, const char * mode );

    打开文件。即采用mode方式,打开filename文件。

    其中,mode有如下形式:

"r"Open a file for reading. The file must exist.
"w"Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a"Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+"Open a file for update both reading and writing. The file must exist.
"w+"Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
"a+"Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek , rewind ) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

     返回值:

              成功:文件流指针

              失败:NULL

1.2 freopen

FILE * freopen ( const char * filename, const char * mode, FILE * stream );

    以不同的打开方式打开文件。

    返回值:

             成功:流指针

             失败:NULL

1.3 fclose

 

int fclose ( FILE * stream );

    关闭文件。即关闭之前打开的stream。

    返回值:

             成功:0

             失败:EOF

二. 读

2.1 fread

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

    从stream(文件流)中读取count个大小为size的数据到ptr中。即从文件的当前定位位置开始,读取size*count的数据,存放在ptr指向的内存中。

    返回值:读取到的块数。如果返回结果与count不相等,那么可能出现了:读取错误 or 读到文件尾部。

   You can use either ferror or feof to check whether an error happened or the End-of-File was reached.

2.2 fgetc

int fgetc ( FILE * stream );

    从流中读取一个字符。

    返回值:

             成功:字符

             失败:EOF(可能是读到文件的尾部或者出错)

2.3 fgets

 

char * fgets ( char * str, int num, FILE * stream );

     从stream中读取字符串。正常情况下,从Stream中读取num-1个字符。如果遇到EOF、换行,就结束读取。

     返回值:

             成功:str

             失败:NULL

2.4 fscanf

 

int fscanf ( FILE * stream, const char * format, ... );

     读取格式化的数据。

     返回值:

             成功:个数(读取成功的)

             失败:EOF

 

三.写

3.1 fwrite

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

    写块数据到流中。将ptr指向的count个size大小的数据写入到sttream。

    返回值:

             成功:count

             失败:!=count

3.2 fputc

int fputc ( int character, FILE * stream );

    把字符写入stream。写入一个字符,并将stream定位到原来字符位置(即后移一位)

    返回值:

             成功:character

             失败:EOF

3.3 fputs

int fputs ( const char * str, FILE * stream );

    将str指定的字符串写入stream。

    返回值:

             成功:非负数

             失败:EOF

3.4 fprintf

int fprintf ( FILE * stream, const char * format, ... );

    将格式化数据,写入文件。

    返回值:

              成功:写入的字节数

              失败:负数

 

四、定位

4.1  fseek

 

int fseek ( FILE * stream, long int offset, int origin );

    重定位流指示器。在stream中,以origin为基准,偏移offset。

    其中,offset整数表示正向偏移,负数表示负向偏移。origin可选择如下三值:

 

SEEK_SETBeginning of file
SEEK_CURCurrent position of the file pointer
SEEK_ENDEnd of file

    返回值:

               定位成功:0

               不成功    :非0

4.2 fgetops

int fgetpos ( FILE * stream, fpos_t * position );

    获取stream的当前位置。

    其中,postion是存储流位置的结构数据。

    返回值:

              成功:0

              失败:!0

4.3 fsetops

int fsetpos ( FILE * stream, const fpos_t * pos );

    设置stream的位置指示器。

    返回值:

             成功:0

             失败:!0

/* fsetpos example */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  fpos_t position;

  pFile = fopen ("myfile.txt","w");
  fgetpos (pFile, &position);
  fputs ("That is a sample",pFile);
  fsetpos (pFile, &position);
  fputs ("This",pFile);
  fclose (pFile);
  return 0;
}

五、其他

 5.1 fflush

int fflush ( FILE * stream );

    清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件。

    返回值:

             成功:0

             失败:EOF

 

/*   FFLUSH.C   */  

#include   <stdio.h>  
#include   <conio.h>  

void   main(   void   )  
{  
	int   integer;  
	char   string[81];  

	/*   Read   each   word   as   a   string.   */  
	printf(   "Enter   a   sentence   of   four   words   with   scanf:   "   );  
	for(   integer   =   0;   integer   <   4;   integer++   )  
	{  
		scanf(   "%s ",   string   );  
		printf(   "%s\n ",   string   );  
	}  

	/*   You   must   flush   the   input   buffer   before   using   gets.   */  
	fflush(   stdin   );  
	printf(   "Enter   the   same   sentence   with   gets:   "   );  
	gets(   string   );  
	printf(   "%s\n ",   string   );  
}  

5.2 feof

 

int feof ( FILE * stream );

     检测文件流指示位置是否在尾部。

     返回值:

              在  :!0

              不在:0

5.3 ferror

 

int ferror ( FILE * stream );

    错误检测。

    返回值:

             有错误:!0

              没错误:0

 

 

参考资料:

1. stdio.h

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值