一个文件操作的简单程序:关于文件的fopen、fread、fseek、ftell、fclose等操作(FIFO related)

来源:互联网 发布:sop8单片机 编辑:程序博客网 时间:2024/06/05 06:15

原文出处:http://blog.csdn.net/CHANGQINGTENGCJM/article/details/48346069

文件的读写能大大提高linux程序编写的效率,减少代码量的同时,可以简化程序逻辑,在设计API交互时应用普遍。下面,我们将通过笔者编写的一个程序来对这个过程做一个初步的讲解,同时也希望通过对改程序的学习,能加强大家对文件的一些基本操作的认识。

linux系统下,文件是基本组成单元,而文件指针则是最常见的操作单位,常常通过文件描述符(FD:file description)来对文件进行操作。文件操作所需要的源文件为“#include<stdio.h>”,关于文件操作函数的定义可以参考以下链接,更加细致:

链接1:文件操作函数

链接2: 函数库查询入口

本文所讲解函数的下载链接:FIFO_write

通过fopen操作将返回该文件的操作符即FD,该函数定义如下:

定义函数: FILE * fopen(const char * path,const char * mode);

函数说明: 参数path字符串包含欲打开的文件路径及文件名,参数mode字符串则代表着流形态,如读、写等。

返回值:文件顺利打开后,指向该流的文件指针就会被返回。若果文件打开失败则返回NULL,并把错误代码存在errno 中。

fclose则与fopen对应,文件操作后一定记得加上fclose,该函数定义如下:

定义函数: int fclose(FILE * stream);

函数说明: fclose()用来关闭先前fopen()打开的文件。此动作会让缓冲区内的数据写入文件中,并释放系统所提供的文件资源。

返回值: 若关文件动作成功则返回0,有错误发生时则返回EOF并把错误代码存到errno。

此外,文件打开后,文件操作指针将指向开始处,此时可以使用fseek和ftell来对文件内的某个位置做基本操作,例如通过fread和fwrite读写文件内容,或通过ftell统计某一段的字节数。关于fseek和ftell函数定义如下:

定义函数 int fseek(FILE * stream,long offset,int whence);  详细链接:fseek用法

函数说明 fseek()用来移动文件流的读写位置。参数stream为已打开的文件指针,参数offset为根据参数whence来移动读写位置的位移数。

返回值 当调用成功时则返回0,若有错误则返回-1,errno会存放错误代码。

定义函数 long ftell(FILE * stream);   详细链接:ftell用法一例(本链接是一个例子)

函数说明 ftell()用来取得文件流目前的读写位置。参数stream为已打开的文件指针。

返回值 当调用成功时则返回目前的读写位置,若有错误则返回-1,errno会存放错误代码。

此外,可以通过fread和fwrite向文件中读写内容,本例用到的是fread,其定义为:

定义函数 size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream);详细链接:fwrite与fread的用法

函数说明 fwrite()用来将数据写入文件流中。参数stream为已打开的文件指针,参数ptr 指向欲写入的数据地址,总共写入的字符数以参数size*nmemb来决定。Fwrite()会返回实际写入的nmemb数目。

返回值 返回实际写入的nmemb数目。

定义函数 size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream);

函数说明 fread()用来从文件流中读取数据。参数stream为已打开的文件指针,参数ptr 指向欲存放读取进来的数据空间,读取的字符数以参数size*nmemb来决定。Fread()会返回实际读取到的nmemb数目,如果此值比参数nmemb 来得小,则代表可能读到了文件尾或有错误发生,这时必须用feof()或ferror()来决定发生什么情况。

返回值 返回实际读取到的nmemb数目。

下面为例程讲解部分:(讲解通过注释的方式进行)

[objc] view plaincopy
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <stdint.h>  
  5. #include <string.h>  
  6. #include <sys/stat.h>  
  7. #include <sys/types.h>  
  8. #include <fcntl.h>  
  9. uint32_t OpBuf[11024 * 4];  
  10. FILEFILE *tFp = NULL;  
  11.   
  12. int main(int argc, charchar **argv)  
  13. {  
  14.     //there should be 2 arguments and then open the file in read mode  
  15.     if(argc != 2){  
  16.         fprintf(stderr,"usage:%s srcfile\n",argv[0]);  
  17.         exit(EXIT_FAILURE);  
  18.     }  
  19.     tFp = fopen(argv[1], "r+");  
  20.     //return the fd.  
  21.     if (tFp == NULL)  
  22.     {  
  23.         printf("Error: open error\n");            
  24.         exit(EXIT_FAILURE);   
  25.     }  
  26.     //count the bytes of file (usage of fseek and ftell) one char one byte including '\n' maybe also '\r'  
  27.     fseek(tFp,0L,SEEK_END);  
  28.     printf("file bytes counts %ld\n",ftell(tFp));  
  29.     //place the file pointer to the start place and be ready to read the bytes to buffer  
  30.     fseek(tFp,0,SEEK_SET);  
  31.     int count = 0;  
  32.     char buf[10];  
  33.     int upperLen = 2;//the total lines  
  34.     int read_count=0;  
  35.     while (!feof(tFp) && count < upperLen)  
  36.     {  
  37.         memset(buf, 010);//clear the buffer  
  38.         read_count = fread(&buf, 91, tFp);//read 10 bytes 1 time from fd into buffer  
  39.         if(read_count){//if bytes read, transfer the bytes to hex and save in OpBuf, and print the bytes   
  40.             OpBuf[count] = strtoul(buf, NULL16);//two previous char one byte   
  41.             printf("read %d bytes, 0x%08x(%d)\n", read_count,OpBuf[count],count);  
  42.         }  
  43.         count++;  
  44.         sleep(1);  
  45.     }  
  46.     fclose(tFp);//close the file  
  47.     //send the bytes to FIFO  
  48.     int fd ;  
  49.     fd = open("/tmp/FIFO-OUT",O_WRONLY|O_NONBLOCK);//open FIFO in read-only and non-block  
  50.     if(fd == -1){  
  51.         perror("open error");  
  52.         exit(EXIT_FAILURE);  
  53.     }  
  54.     //char buf[1024*4];  
  55.     int n = 0;  
  56.     n = write(fd,(uint8_t *)OpBuf,upperLen*4);//write the bytes into fifo by byte and the last param is total bytes  
  57.     if (n == -1 )  
  58.         printf("Null\n");  
  59.     else printf("write %d bytes\n",n);  
  60.     close(fd);//close fifo  
  61.     printf("write success\n");  
  62.     return 0;  
  63. }  
看完这个例程希望大家能有所收获!
0 0
原创粉丝点击