当前位置:网站首页>Operation of C language files
Operation of C language files
2022-07-19 05:31:00 【Learn to put down ta】
C Operation of language files
Document classification : text file 、 Binary
Open file fopen( Path and file name , Turn on mode )
If it is opened successfully, it will return to FILE Pointer to structure , Otherwise return to null Fixed error
Because the system newline character is the same as C Different language , So we should distinguish between text files and binary files , Only Unix Is with the C The language is the same , There is no need to distinguish between file types .
1.“r” Pattern :
1.1 Open the file for “ read-only ” operation , That is, you can only read content from files .
1.2 If the file you want to operate does not exist , Open failed .
1.3 When the file is successfully opened , The file pointer is at the beginning of the file .
1.4 After opening the file , The original contents in the file will not be emptied .
1.5 The content can be read from anywhere in the file .
2."w" Pattern :
2.1 Open the file for “ Just write ” operation , That is, only the contents can be written to the file .
2.2 If the file you want to operate does not exist , Create a new file .
2.3 When the file is successfully opened , The file pointer is at the beginning of the file .
2.4 After opening the file , The original contents in the file will be emptied .
2.5 You can write content anywhere in the file , And when writing , It will overwrite the contents of the original location .
3."a" Pattern :
3.1 Open the file for “ Additional ” operation , That is, only the contents can be written to the file .
3.2 If the file you want to operate does not exist , Create a new file .
3.3 When the file is successfully opened , The file pointer is at the end of the file .
3.4 After opening the file , The original contents in the file will not be emptied .
3.5 Can only be appended to the end of the file ( Write ) Content .
4."r+" Pattern :
4.1 Open the file for “ Reading and writing ” operation , That is, it can be read , Write again .
4.2 If the file you want to operate does not exist , Open failed .
4.3 When the file is successfully opened , The file pointer is at the beginning of the file .
4.4 After opening the file , The original contents in the file will not be emptied .
4.5 Whether reading or writing content , Can be done anywhere in the file , And when writing , It will overwrite the contents of the original location .
5."w+" Pattern :
5.1 Open the file for “ Reading and writing ” operation , That is, it can be read , Write again .
5.2 If the file you want to operate does not exist , Create a new file .
5.3 When the file is successfully opened , The file pointer is at the beginning of the file .
5.4 After opening the file , The original contents in the file will be emptied .
5.5 Whether reading or writing content , Can be done anywhere in the file , And when writing , It will overwrite the contents of the original location .
6."a+" Pattern :
6.1 Open the file for “ Reading and writing ” operation , That is, it can be read , Write again .
6.2 If the file you want to operate does not exist , Create a new file .
6.3 When the file is successfully opened , The file pointer is at the end of the file .
6.4 After opening the file , The original contents in the file will not be emptied .
6.5 When reading content , It can be done at any position , But when writing content , It will only be appended at the end of the file .

Close file fclose(FILE*)
Read and write a single character
1、 read fgetc(FILE*)
Read unsigned char return int type , If the file ends or an error is encountered, return EOF,fgetc() And getc() The two functions are consistent , The difference is that fgetc() It's a function ,getc() Is the implementation of macros , Macro is a little faster , But macros may call parameters repeatedly , Parameters with side effects such as fp++ It may become fp++++++
2、 Write fputc(int c,FILE*)
c Is the character to be written , Successful return of written characters , If there is an error, return EOF,fputc() And putc() The difference is the same as 1
Read and write the whole string
1、fgets(char * s,int n,FILE*)
s Is the storage address for reading characters ,int Is the number of reads n-1, Leave a plus \0, If there is an error, it will not overwrite s The content of the string pointed to
2、fputs(char *s,FILE*)
The parameters are the same as above
Format read-write file
1、 write in :fprint(FILE*,“%d%f…”,int ,float…)
2、 Read :fscanf(FILE*, Read in format , Data storage location )
FILE* fp;
int a;
fscanf(fp,"%d",&a);// A number of the read file is stored in a in
Binary read-write :
fread() And fwrite() They all have four parameters ,ptr Is the data storage address pointer , The second is to read the memory block size , The third is to read the number , The last one is the file pointer
FILE *fp;
struct book *tm=(struct book*)malloc(sizeof(struct book));
// Initialization omitted
fwrite(tm,sizeof(struct book),1,fp);// hold tm The data in is written in fp in
struct book *um=(struct book*)malloc(sizeof(struct book));
fread(um,sizeof(struct book),1,fp);// hold fp The data read in is stored in um in
Random, speaking, reading and writing
ftell(FILE*), The return value is usually long int type , It is understood that the file is a large array , Return to cursor position
rewind(FILE*), Call this function to move the cursor to the beginning of the file
Move cursor function fseek(FILE*,long int,whence):
FILE*fp;
fseek(fp,4,SEEK_SET);// Move the cursor to four bytes after the beginning of the file
fseek(fp,4,SEEK_CUR);// Move the cursor to four bytes behind the current position
fseek(fp,-4,SEEK_END);// Move the cursor to the first four bytes at the end of the file
Be careful : Before each of the above operations on the file, you must first open the file and then close it ( I omitted ), Prevent data reading and writing errors
边栏推荐
- 12.数据仓库搭建之ADS层搭建
- Redis source code analysis dynamic string implementation (SDS)
- 线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作
- ubantu中gcc编译C语言小问题
- 常量与常量指针
- ETL tool -- kettle realizes simple data migration
- 一次全面的性能优化,从5秒优化到1秒
- List与Map
- Rk356x u-boot Institute (command section) 3.4 usage of MEM memory related commands
- Pointer function of C language
猜你喜欢

Solve the problem of inconsistent prediction effect between text detection training model and information model based on paddleocr

Easypoi之excel模板导出

BUUCTF web WarmUp

【全网首发】JVM性能问题的自动分析

Implementation of synchronization interface of 6 libcurl based on libco

Distributed storage fastdfs

Parent components plus scoped sometimes affect child components

C语言文件的操作

软件测试就业前景怎样 人才需求大,岗位稳定性强

Single arm routing configuration
随机推荐
【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从
How to deal with the mismatch between subtitle files and video files
ambari2.7.5集成es6.4.2
Minor problems of GCC compiling C language in ubantu
Talk about the 8 pits of redis distributed lock
Easypoi之excel多sheet导入
Easypoi excel multi sheet import
Redis source code analysis 3 implementation of discontinuous traversal
idea导入本地包
Data visualization
MySQL学习笔记(4)——(基本CRUD)操作数据库中的表的数据
东软跨境电商数仓开发进度
User mode protocol stack - UDP implementation based on netmap
Log4j的使用
Router loopback port experiment
【全网首发】主线程异常会导致 JVM 退出?
操作系统常见面试题
Nacos configuration management
Beginner's Guide to learning penetration testing
MySQL--存储和游标