当前位置:网站首页>File processing examples of fopen, FREAD, fwrite, fseek
File processing examples of fopen, FREAD, fwrite, fseek
2022-07-04 18:24:00 【Hua Weiyun】
Like any operating system , File processing is Linux Core concepts in . Any system programmer will regard it as him / One of her initial programming tasks is learning . This aspect of programming involves system files .
Through file processing , You can create system files 、 modify 、 Delete and other operations . In this paper , Try to introduce very basic file processing .
File processing function
In this paper , We will introduce the following functions commonly used in file processing :
fopen()
FILE *fopen(const char *path, const char *mode);fopen() Function to open a file and put I/O Flow and its association . This function has two arguments . The first parameter is a pointer to the string containing the file name to open , The second parameter is the mode to open the file . The pattern can be :
- 'r' : Open a text file to read . The stream is at the beginning of the file .
- 'r+' : Turn on read write . The stream is at the beginning of the file .
- 'w' : Truncate the file to zero length or create a text file for writing . The stream is at the beginning of the file .
- 'w+' : Turn on read write . If the file doesn't exist , Then create the file , Otherwise, cut it off . The stream is at the beginning of the file .
- 'a' : Open for attachment ( Write... At the end of the file ). If the file doesn't exist , Then create the file . The stream is at the end of the file .
- 'a+' : Open for reading and attaching ( Write... At the end of the file ). If the file doesn't exist , Then create the file . The initial file position read is at the beginning of the file , But the output is always appended to the end of the file .
fopen() The function returns... On success FILE Flow pointer , And when it fails, it returns NULL.
fread() and fwrite()
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);fread/fwrite The function is used from fopen Function to open the file / Write data . These functions take three parameters . The first parameter is for reading / Pointer to the buffer where data is written . Read / The written data adopts “nmemb” Element form , Every “size” Byte length .
If it works ,fread/fwrite Return from fopen Function to actually read from the stream opened / Bytes written . If you fail , Returns a smaller number of bytes ( Then request to read / Write ).
fseek()
int fseek(FILE *stream, long offset, int wherece);fseek() The function is used to set the file location indicator of the stream to the new location . This function takes three arguments . The first parameter is fopen() Function return FILE Flow pointer . The second parameter 'offset' Tell us the number of bytes to find . The third parameter “whence” Tell me where to look “offset” Number of bytes .wherece The available values of are SEEK_SET、SEEK_CUR or SEEK_END. These three values ( According to the order ) Describes the beginning of the file 、 Current location and end of file .
success , This function returns 0, Otherwise return to -1.
fclose()
int fclose(FILE *fp);fclose() Function first refreshes fopen() Open stream , Then close the underlying descriptor . After successful completion , This function returns 0, Otherwise, it returns to the end of the file (eof). In the event of failure , If you further access the stream , Then the behavior remains undefined .
Code
#include<stdio.h>#include<string.h>#define SIZE 1#define NUMELEM 5int main(void){ FILE* fd = NULL; char buff[100]; memset(buff,0,sizeof(buff)); fd = fopen("test.txt","rw+"); if(NULL == fd) { printf("\n fopen() Error!!!\n"); return 1; } printf("\n File opened successfully through fopen()\n"); if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fd)) { printf("\n fread() failed\n"); return 1; } printf("\n Some bytes successfully read through fread()\n"); printf("\n The bytes read are [%s]\n",buff); if(0 != fseek(fd,11,SEEK_CUR)) { printf("\n fseek() failed\n"); return 1; } printf("\n fseek() successful\n"); if(SIZE*NUMELEM != fwrite(buff,SIZE,strlen(buff),fd)) { printf("\n fwrite() failed\n"); return 1; } printf("\n fwrite() successful, data written to text file\n"); fclose(fd); printf("\n File stream closed through fclose()\n"); return 0;}The above code assumes that you have a test file “test.txt” Put it in the same location where the executable is running .
The content of the original document is :
$ cat test.txthello everybodyNow? , Run code :
$ ./fileHandling File opened successfully through fopen() Some bytes successfully read through fread() The bytes read are [hello] fseek() successful fwrite() successful, data written to text file File stream closed through fclose()Check the file again test.txt The content of . As shown below , The contents of the file have been modified .
$ cat test.txthello everybodyhello边栏推荐
- Easy to use map visualization
- "In Vietnam, money is like lying on the street"
- 提升复杂场景三维重建精度 | 基于PaddleSeg分割无人机遥感影像
- Reptile elementary learning
- Stars open stores, return, return, return
- 7 RSA Cryptosystem
- 大厂面试总结大全二
- 12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
- “在越南,钱就像躺在街上”
- android使用SQLiteOpenHelper闪退
猜你喜欢
随机推荐
celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
估值900亿,超级芯片IPO来了
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
基于NCF的多模块协同实例
【Hot100】32. 最长有效括号
Journal des problèmes de brosse à boutons de force / day6 / 6.28
Li Kou brush question diary /day7/6.30
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
【211】go 处理excel的库的详细文档
Thawte通配符SSL证书提供的类型有哪些
Machine learning concept drift detection method (Apria)
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
Redis主从复制
Load test practice of pingcode performance test
Russia arena data releases PostgreSQL based products
Face_ Attendance statistics of recognition face recognition
Blue bridge: sympodial plant
[system analyst's road] Chapter 7 double disk system design (structured development method)
Reptile elementary learning
能源行业的数字化“新”运维









