当前位置:网站首页>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
边栏推荐
- Automatic tracking
- ROS通信机制进阶
- 无法启动程序,拒绝访问?
- Reading and writing of C # file
- Pzk learns data types, binary conversion, input and output, operators, branch statements, ifelse of C language
- 如何选择正确的服务器备份方法
- 多线程的相关知识
- UnityShader-深度纹理(理解以及遇到的问题)
- Pzk learns string function of C language (1)
- Unity engine starts to migrate from mono to.Net coreclr
猜你喜欢

Allow or prohibit connecting to a non domain and a domain network at the same time

Remote sensing image recognition imaging synthesis

Briefly remember the top ten orders

Random points in non overlapping rectangle (force deduction daily question)

Unity shader overview

wireshark IP地址域名解析

数据库的联合查询

Code implementation and introduction of all commonly used sorting

wireshark数据包修改--IP地址修改(一)

5g's past and present life -- a brief introduction to the development of mobile communication
随机推荐
wireshark图形界面介绍
Multi threaded CAS, synchronized lock principle, JUC and deadlock
ROS通信机制进阶
Programming learning records - Lesson 3 [first knowledge of C language]
[first blog - outlook]
技术和理论知识学习的一点心得
Code implementation and introduction of all commonly used sorting
5G网络身份识别---详解5G-GUTI
The principle of hash table and the solution of hash conflict
5G的前世今生---简述移动通信的发展
ROS工作空间覆盖
自动化部署项目
Programming learning records - Lesson 4 [branch and loop statements]
Shell script if nested for loop script
Man moon myth reading notes
软件测试用里篇
Reading and writing of file content - data flow
Automated Deployment Project
C thread lock
5g's past and present life -- a brief introduction to the development of mobile communication