当前位置:网站首页>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 everybody
Now? , 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
边栏推荐
- 用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
- "In Vietnam, money is like lying on the street"
- Set the transparent hidden taskbar and full screen display of the form
- 力扣刷题日记/day8/7.1
- [system disk back to U disk] record the operation of system disk back to U disk
- 力扣刷题日记/day7/2022.6.29
- 大厂面试总结大全二
- Load test practice of pingcode performance test
- TCP两次挥手,你见过吗?那四次握手呢?
- Heartless sword Chinese translation of Elizabeth Bishop's a skill
猜你喜欢
How to improve development quality
“在越南,钱就像躺在街上”
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
【Hot100】32. Longest valid bracket
With an estimated value of 90billion, the IPO of super chip is coming
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
力扣刷题日记/day7/6.30
Li Kou brush question diary /day7/2022.6.29
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
随机推荐
五千字讲清楚团队自组织建设 | Liga 妙谈
如何进行MDM的产品测试
Clever use of curl command
如何使用 wget 和 curl 下载文件
Summary of subsidy policies across the country for dcmm certification in 2022
SIGMOD’22 HiEngine论文解读
"In Vietnam, money is like lying on the street"
android使用SQLiteOpenHelper闪退
[211] go handles the detailed documents of Excel library
【Hot100】32. Longest valid bracket
Is it safe to download the mobile version of Anxin securities and open an account online
Numpy 的仿制 2
regular expression
General environmental instructions for the project
Heartless sword Chinese translation of Elizabeth Bishop's a skill
通过事件绑定实现动画效果
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
【211】go 处理excel的库的详细文档
Redis master-slave replication
Superscalar processor design yaoyongbin Chapter 7 register rename excerpt