当前位置:网站首页>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边栏推荐
- 如何进行MDM的产品测试
- 五千字讲清楚团队自组织建设 | Liga 妙谈
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
- Lua emmylua annotation details
- Recast of recastnavigation
- Five thousand words to clarify team self-organization construction | Liga wonderful talk
- Russia arena data releases PostgreSQL based products
- Load test practice of pingcode performance test
- You should know something about ci/cd
- Mysql5.7 installation tutorial graphic explanation
猜你喜欢

爬虫初级学习

ISO27001认证办理流程及2022年补贴政策汇总

With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute

Numpy 的仿制 2

Grain Mall (I)

股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议

华为云ModelArts的使用教程(附详细图解)

I wrote a learning and practice tutorial for beginners!

Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)

输入的查询SQL语句,是如何执行的?
随机推荐
力扣刷题日记/day8/7.1
LD_ LIBRARY_ Path environment variable setting
android使用SQLiteOpenHelper闪退
【系统盘转回U盘】记录系统盘转回U盘的操作
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
fopen、fread、fwrite、fseek 的文件处理示例
如何进行MDM的产品测试
[209] go language learning ideas
Achieve animation effect through event binding
Load test practice of pingcode performance test
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
Imitation of numpy 2
Win32 API access route encrypted web pages
创业两年,一家小VC的自我反思
网上开户安全吗?是真的吗?
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
I wrote a learning and practice tutorial for beginners!
90后开始攒钱植发,又一个IPO来了