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

论坛 期权论坛 脚本     
匿名技术用户   2021-1-5 08:51   11   0

文件的读写能大大提高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数目。

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

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
uint32_t OpBuf[1024 * 4];
FILE *tFp = NULL;

int main(int argc, char **argv)
{
 //there should be 2 arguments and then open the file in read mode
 if(argc != 2){
  fprintf(stderr,"usage:%s srcfile\n",argv[0]);
  exit(EXIT_FAILURE);
 }
 tFp = fopen(argv[1], "r+");
 //return the fd.
 if (tFp == NULL)
 {
  printf("Error: open error\n");   
  exit(EXIT_FAILURE); 
 }
 //count the bytes of file (usage of fseek and ftell) one char one byte including '\n' maybe also '\r'
 fseek(tFp,0L,SEEK_END);
 printf("file bytes counts %ld\n",ftell(tFp));
 //place the file pointer to the start place and be ready to read the bytes to buffer
 fseek(tFp,0,SEEK_SET);
 int count = 0;
 char buf[10];
 int upperLen = 2;//the total lines
 int read_count=0;
 while (!feof(tFp) && count < upperLen)
 {
  memset(buf, 0, 10);//clear the buffer
  read_count = fread(&buf, 9, 1, tFp);//read 10 bytes 1 time from fd into buffer
  if(read_count){//if bytes read, transfer the bytes to hex and save in OpBuf, and print the bytes 
   OpBuf[count] = strtoul(buf, NULL, 16);//two previous char one byte 
   printf("read %d bytes, 0x%08x(%d)\n", read_count,OpBuf[count],count);
  }
  count++;
  sleep(1);
 }
 fclose(tFp);//close the file
 //send the bytes to FIFO
 int fd ;
 fd = open("/tmp/FIFO-OUT",O_WRONLY|O_NONBLOCK);//open FIFO in read-only and non-block
 if(fd == -1){
  perror("open error");
  exit(EXIT_FAILURE);
 }
 //char buf[1024*4];
 int n = 0;
 n = write(fd,(uint8_t *)OpBuf,upperLen*4);//write the bytes into fifo by byte and the last param is total bytes
 if (n == -1 )
  printf("Null\n");
 else printf("write %d bytes\n",n);
 close(fd);//close fifo
 printf("write success\n");
 return 0;
}
看完这个例程希望大家能有所收获!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP