UNIX常用文件函数使用指南

一、ANSI_stdio

以下是UNIX系统下的一些文件操作函数:

clearerr()- clear end-of-file and error flags for a stream (ANSI)
fclose()  - close a stream (ANSI)
fdopen()  - open a file specified by a file descriptor (POSIX)
feof()    - test the end-of-file indicator for a stream (ANSI)
ferror()  - test the error indicator for a file pointer (ANSI)
fflush()  - flush a stream (ANSI)
fgetc()   - return the next character from a stream (ANSI)
fgetpos() - store the current value of the file position indicator for a stream (ANSI)
fgets()   - read a specified number of characters from a stream (ANSI)
fileno()  - return the file descriptor for a stream (POSIX)
fopen()   - open a file specified by name (ANSI)
fprintf() - write a formatted string to a stream (ANSI)
fputc()   - write a character to a stream (ANSI)
fputs()   - write a string to a stream (ANSI)
fread()   - read data into an array (ANSI)
freopen() - open a file specified by name (ANSI)
fscanf()  - read and convert characters from a stream (ANSI)
fseek()   - set the file position indicator for a stream (ANSI)
fsetpos() - set the file position indicator for a stream (ANSI)
ftell()   - return the current value of the file position indicator for a stream (ANSI)
fwrite()  - write from a specified array (ANSI)
getc()    - return the next character from a stream (ANSI)
getchar() - return the next character from the standard input stream (ANSI)
gets()    - read characters from the standard input stream (ANSI)
getw()    - read the next word (32-bit integer) from a stream
perror()  - map an error number in errno to an error message (ANSI)
putc()    - write a character to a stream (ANSI)
putchar() - write a character to the standard output stream (ANSI)
puts()    - write a string to the standard output stream (ANSI)
putw()    - write a word (32-bit integer) to a stream
rewind()  - set the file position indicator to the beginning of a file (ANSI)
scanf()   - read and convert characters from the standard input stream (ANSI)
setbuf()  - specify the buffering for a stream (ANSI)
setbuffer() - specify buffering for a stream
setlinebuf() - set line buffering for standard output or standard error
setvbuf() - specify buffering for a stream (ANSI)
stdioInit()- initialize standard I/O support
stdioFp() - return the standard input/output/error FILE of the current task
stdioShowInit() - initialize the standard I/O show facility
stdioShow() - display file pointer internals
tmpfile()  - create a temporary binary file (Unimplemented) (ANSI)
tmpnam()   - generate a temporary file name (ANSI)
ungetc()   - push a character back into an input stream (ANSI)
vfprintf() - write a formatted string to a stream (ANSI)

二、常用的标准C库文件操作函数

1.fopen() 打开文件

FILE * fopen
    (
    const char * file,   /* name of file */
    const char * mode    /* mode */
    ) 
    *file 文件名及路径
	*mode 文件权限

      字符                含义
      
       "r"           打开文字文件只读
       "w"           创建文字文件只写
       "a"           追加, 若文件不存在则创建
       "r+"          打开一个文字文件读/"w+"          创建一个文字文件读/"a+"          打开或创建一个文件增补
       "b"           二进制文件
       "t"           文这文件(默认项) 
       "ab"			 打开或创建二进制文件以在文件末尾写入
       "rb"			 打开二进制文件以供读取
       "wb"  		 打开二进制文件以供写入
       "ab+"/"a+b"	 打开二进制文件进行更新
       "rb+"/"r+b"	 打开一个只读二进制文件,允许数据读写
       "wb+"/"w+b"   打开一个只写二进制文件,允许数据读写

代码如下(示例):

	FILE *fp;         //定义一个文件指针
    fp=fopen("file_name", "w+");  /*打开或创建一个文字文件读或写*/
    打开一个文件成功返回文件指针, 否则返回空指针 (NULL)

2.fclose() 关闭文件

int fclose
	( 
	FAST FILE * fp   /* stream to close */ 
	)  
	*fp 已存在的文件指针

代码如下(示例):

 	fclose(fp);
	返回一个整型数,若文件关闭成功返回0, 否则返回一个非零值

3.fwrite() 写数据

size_t fwrite
    (
    const void * buf,     /* where to copy from */
    size_t       size,    /* element size */
    size_t       count,   /* no. of elements */
    FILE *       fp       /* stream to write to */
    )    
    const void*buf :buf
	size_t size    :类型大小
	size_t count   : 读取个数
	fp :文件指针/句柄            

代码如下(示例):

	fwrite(buf,128,1,fp);
	从buf中写一次128字节的内容到fp指针所绑定的文件

4.fread() 读数据

size_t fread
    (
    void *      buf,     /* where to copy data */
    size_t      size,    /* element size */
    size_t      count,   /* no. of elements */
    FAST FILE * fp       /* stream to read from */
    )  
    void *      buf,     读出数据存储buf
    size_t      size,    读取一次的大小
    size_t      count,   读取次数
    FAST FILE * fp       文件指针 

代码如下(示例):

  fread(buf,len,1,fp);
  从文件指针所绑定的文件中读取一次len长度的内容存到buf中

5.fseek() 设置文件指针的位置

int fseek
    (
    FAST FILE * fp,       /* stream */
    long        offset,   /* offset from <whence> */
    int         whence    /* position to offset from: */
                          /* SEEK_SET = beginning */
                          /* SEEK_CUR = current position */
                          /* SEEK_END = end-of-file */
    )
    FAST FILE * fp,      文件指针
    long        offset,  偏移量(正/负)
    int         whence 	指针位置
    whence :SEEK_SET    文件开头
    		SEEK_CUR	当前位置
    		SEEK_END 	文件末尾

代码如下(示例):

  fseek(fp,5,SEEK_SET);
  将文件指针从开头向后偏移5个位置

  fseek(fp,-5,SEEK_END);
  将文件指针从末尾向前偏移5个位置

6.fscanf() 从文件流中读数据并格式化

int fscanf
    (
    FILE *       fp,    /* stream to read from */
    char const * fmt,   /* format string */
    ...                 /* arguments to format string */
    )         
    FILE *       fp,    文件指针
    const char * fmt,   字符格式
    ...                 字符参数

代码如下(示例):

 	fscanf(fp,"%d	%d",&arg1,&arg2);
	从fp文件流中读取两个整型数据(注意fmt参数的空格与缩进符)

7.fprintf() 向文件流中写入格式化数据

int fprintf
    (
    FILE *       fp,    /* stream to write to */
    const char * fmt,   /* format string */
    ...                 /* optional arguments to format string */
    )  
    FILE *       fp,    文件指针
    const char * fmt,   字符格式
    ...                 字符参数

代码如下(示例):

 	fprintf(fp,"%d	%d",arg1,arg2);
	向fp文件流中写入两个整型数据

8.fgetc()/fgets() 从文件流中读取字符或字符串

int fgetc
    (
    FILE * fp   /* stream to read from */
    )
    FILE * fp 文件指针
    
    char * fgets
    (
    char *      buf,   /* where to store characters */
    FAST int    n,     /* no. of bytes to read + 1 */
    FAST FILE * fp     /* stream to read from */
    )   
    char *      buf,   存储读出来的数据
    FAST int    n,     读取n+1字节的数据
    FAST FILE * fp     文件指针   

代码如下(示例):

	fgetc(fp);
	从fp文件流中读取一个字符
	
	fgets(buf,10,fp);
	从fp文件流中读取10个字符存到buf中

9.fputc()/fputs() 向文件流中写入字符或字符串

	int fputc
	    (
	    int         c,   /* character to write */
	    FAST FILE * fp   /* stream to write to */
	    )   
 		int         c,   写入字符
	    FAST FILE * fp   写入的文件指针
	int fputs
	    (
	    const char * s,   写入字符串
	    FILE *       fp   写入的文件指针
	    )                 

代码如下(示例):

	fputc("A",fp);
	向fp文件流中写入一个字符
	
	fputs("ABCDEFG",fp);
	从fp文件流中写入字符串

10 feof() 判断文件末尾

int feof
    (
    FILE * fp   /* stream to test */
    )   
    FILE *fp	文件指针

代码如下(示例):

	feof(fp);
	文件结束:返回非0值;文件未结束:返回0

总结

以上简单介绍了UNIX/Linux/VxWorks系统中一些常用的文件操作函数使用,如有错误,请及时联系作者修正。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值