当前位置:网站首页>C language - file operation
C language - file operation
2022-07-27 06:23:00 【Autumn mountain chariot God AE】
The file pointer :FILE
FILE yes C A structure type declared by a language system , The relevant information of the document will be stored in FILE In variables of type , After opening the file , Maintain the opened file through this structure variable .
After opening a file , The system will automatically create a FILE Variable of type , Through the file pointer variable, you can find the file associated with it .
Opening and closing of files :
You need to open the file before reading or writing it , You need to close the file after using it .
Use fopen Function to open a file , Use fclose Function to close the file .
fopen The function needs to pass two string type parameters , They are file names , And open mode
Turn on mode
How files are used meaning If the specified file does not exist
“r”( read-only ) To enter data , Open an existing text file error
“w”( Just write ) To output data , Open a text file Create a new file
“a”( Additional ) Add data to the end of the text file Create a new file
“rb”( read-only ) To enter data , Open a binary file error
“wb”( Just write ) To output data , Open a binary file Create a new file
“ab”( Additional ) Add data to the end of a binary file error
“r+”( Reading and writing ) For reading and writing , Open a text file error
“w+”( Reading and writing ) For reading and writing , Suggest a new file Create a new file
“a+”( Reading and writing ) Open a file , Read and write at the end of the file Create a new file
“rb+”( Reading and writing ) Open a binary file for reading and writing error
“wb+”( Reading and writing ) For reading and writing , Create a new binary file Create a new file
“ab+”( Reading and writing ) Open a binary file , Read and write at the end of the file Create a new file If it opens successfully ,fopen The function will return a pointer , Need to use file Pointer variables of type receive . Thus, the connection between the file and the pointer is established .
If opening fails , Will return a null pointer NULL, Therefore, you need to judge whether it is successfully opened .
After the file is used , Use fclose function , Pass a pointer to the file , You can close the file , Then you need to set the pointer to null .
Sequential reading and writing of files
function Function name Apply to
Character input function fgetc All input streams
Character output function fputc All output streams
Text line input function fgets All input streams
Text line output function fputs All output streams
Format input function fscanf All input streams
Format output function fprintf All output streams
Binary input fread file
Binary output fwrite file fgetc: From the specified stream stream Read a character , Return to one int This character of type . If reading fails or an error is encountered , Then return to EOF.
fputc: Passing one character ASCII Code value and an output stream , Write this character to the input stream .
fgets: From the specified stream stream Read a line , Maximum read n Characters , And store it in str Within the string pointed to . When reading (n-1) In characters , Or when a newline character is read , Or at the end of the file , It will stop . return NULL.
fputs: Write to the specified stream str The string in the string pointed to , Terminate with a null character . If the write fails , Then return to EOF.
fscanf: Input data from the specified stream in the specified format .
fprintf: Input the data into the specified stream through the specified format .
fwrite: Write the specified data to the specified stream in binary form , You need to specify the size and number of data elements to be written .
fread: Data will be read from the specified stream , Write to the specified variable in binary form , You need to specify the size and number of elements to write data .
Random reading and writing of documents
fseek function , You can locate the file pointer by the position and offset of the file pointer .
fseek ( pFile , 9 , SEEK_SET );
// The file pointer // Offset // Which position to offset
SEEK_CUR// The current location of the file pointer
SEEK_END// End of file pointer
SEEK_SET// File pointer start position After locating the file pointer , You can read and write data in the file pointer .
int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "wb" );
fputs ( "This is an apple." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( " sam" , pFile );
fclose ( pFile );
return 0; }ftell function : Returns the offset of the file pointer from the starting position .
rewind function : Set the file pointer to the starting position .
Text files and binaries
Data is stored in binary form in memory , Output to file without processing , Then the file is binary .
With ASCII The file stored in the form of code value is a text file .
Determination of the end of file reading
Out-of-service feof To directly determine whether the reading of the file is over .
feof Only when it is read to the end of the file will it return eof, When an error occurs during reading, the file reading will also end .
Whether the reading of text file is finished , Determine whether the return value is EOF ( fgetc ), perhaps NULL ( fgets )
File buffer
边栏推荐
- Unityshader depth texture (understanding and problems encountered)
- Progress in remote sensing image recognition 2022/5/5
- 接口测试概念及Postman工具简介使用
- The principle of hash table and the solution of hash conflict
- Li Kou daily question leetcode 513. find the value in the lower left corner of the tree
- IP core summary
- Understand the pointer in a picture
- Thesis writing (harvest)
- 通信机制比较
- OSG environment construction (win10+vs2019)
猜你喜欢
随机推荐
Code implementation and introduction of all commonly used sorting
文件内容的读写——数据流
ROS分布式通信
Advanced ROS communication mechanism
Interpretation of unity desktop version 7.6
5G的前世今生---简述移动通信的发展
Programming learning records - Lesson 6 [functions]
Force deduction problem solving monotonous stack
自动化部署项目
软件测试用里篇
Wireshark graphical interface capture
Dynamic planning for solving problems (3)
Wireshark function introduction
shell script if嵌套for循环脚本
遥感影像识别-制作数据集
机器人导航实现
IP核之ROM
Strategies for common locks in multithreading
数据库的索引和事务(重点)
多线程CAS、synchronized锁原理 、JUC以及死锁








