In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the C language how to achieve file operation, the article is very detailed, has a certain reference value, interested friends must read it!
What is a document
In programming, we generally talk about two kinds of documents: program files and data files.
Program file:
Includes source program files (suffix .c), destination files (windows environment suffix .obj), and executable programs (windows environment suffix .exe).
Data file:
The content of the file is not necessarily the program, but the data read and written when the program is running, such as the file from which the program needs to read the data.
Or a file that outputs the content.
Data files are divided into "text files" and "two-tier files".
Binary file: the data is stored in binary form in memory. If the data is output to external memory without conversion, this kind of file is a mess of symbols that only the computer can read.
Text file: if it is required to be stored in ASCII code on external memory, it needs to be converted before storage. In ASCII character form
The stored file is a text file. We can understand this kind of document, such as "word", "letter" and "number" that we see in notepad. Such a document is a text file.
II. File buffer
ANSIC uses a "buffered file system" to process data files, which creates a "file buffer" for each file in use. When the program outputs data from memory to the disk, it will send the data to the output buffer first, and then send the data to the disk together when the output buffer is full; when the program reads data from the disk to memory, the data will be sent to the input buffer first, and then the data will be sent to the disk together when the input buffer is full; in addition, when the program ends, the contents of the buffer will also be sent to memory or disk.
III. Document pointer
Each used file opens up a corresponding file information area in memory to store information about the file (such as the file's
Name, file status and current location of the file, etc. This information is stored in a structural variable. The structure type is
Declared by the system, it is named FILE.
The structure is declared as follows:
Struct _ iobuf {char * _ ptr; int _ cnt; char * _ base; int _ flag; int _ file; int _ charbuf; int _ bufsiz; char * _ tmpfname;}; typedef struct _ iobuf FILE
Whenever you open a file, the system automatically creates a variable of the FILE structure according to the situation of the file, and we can communicate with the
Maintain the variable of the FILE structure by creating a pointer to FILE.
FILE* pf;// file pointer variable IV, file opening and closing.
The file is opened with the fopen function, and the file is closed with the fclose function.
Their prototypes are as follows:
FILE * fopen (const char * filename, const char * mode)
Int fclose (FILE * stream)
Filenname: this parameter is filled with the file name.
Mode: this parameter represents the operation to be performed on the file
Stream: this parameter represents a pointer to the file to be closed.
Files can be operated in the following ways:
"r" (read-only): the program reads data from a text file, and an error occurs if the specified file does not exist.
"w" (write only): the program outputs data to a text file and creates a new file if the specified file does not exist.
"a" (append): add data to the end of the text file, which will cause an error if the specified file does not exist
"rb" (read-only): the program reads data from a file, except that the file is binary, and an error will occur if the specified file does not exist.
"wb" (write only): the program outputs data to a file, but the file is binary, and a new file is created if the specified file does not exist.
"ab" (append): adds data to the end of a binary file, and an error occurs if the specified file does not exist
"r +" (read and write): for reading and writing, this file is a text file, and an error will occur if the file does not exist
"W +" (read and write): for reading and writing, a new file is created if the file does not exist.
"a +" (read and write): open a file, read and write at the end of the file, and create a new file if the file does not exist
"rb+" (read and write): for reading and writing, open a binary file, which will cause an error if the file does not exist
"wb+" (read and write): in order to read and write a binary file, a new file is created if the file does not exist
"ab+" (read and write): read and write at the end of the binary file will create a new file if the file does not exist
The read and write functions of the file are (these functions are read and written sequentially):
Character input function: fgetc
Character output function: fputc
Text line input function: fgets
Text line output function: fputs
Format input function: fscanf
Formatted output function: fprintf
Binary input: fread
Binary output: fwrite
Let's use the code to understand:
Int main () {/ / Open the file FILE* pf = fopen ("data.txt", "r"); if (pf = = NULL) {/ / return an error message if it is not read. Perror ("fopen"); return-1;} / read the file / close the file fclose (pf); pf = NULL; return 0;}
We did an "r" (read) operation on data.txt because this file was not created in the current folder, so there was an error.
Now we create a diata.txt folder in the current directory, enter the character "abcd", and use the fgetc function to read the data, which reads one character and then moves the pointer to the next.
Int main () {/ / Open the file FILE* pf = fopen ("data.txt", "r"); if (pf = = NULL) {perror ("fopen"); return-1;} / / read the file char ch = fgetc (pf); printf ("% c\ n", ch); ch = fgetc (pf) Printf ("% c\ n", ch); / / close the file fclose (pf); pf = NULL; return 0;}
The result is:
This is because at the beginning, the pf pointer first points to the first character "a". After reading the character "a", the pf pointer goes to the next character, pointing to the character "b". After reading "b", the pf pointer goes on to the next character, pointing to the character "c".
Now use the fgetc function to read the data, which reads a string
Int main () {/ / Open file FILE* pf = fopen ("data.txt", "r"); if (pf = = NULL) {perror ("fopen"); return-1;} / / read file char arr [20] = {0}; fgets (arr, 20, pf) Printf ("% s", arr); / / close the file fclose (pf); pf = NULL; return 0;}
The result is:
This code means to read 20 characters from the file pointed to by pf and save it to the array arr. We know that the file is only 4 characters long, so when the file is not enough characters to be read, the program will only read the contents of the file without reporting an error.
Let's do the "w" operation and use the fputc and fputs functions, respectively.
Fputc function
Int main () {FILE* pf = fopen ("test.txt", "w"); if (NULL = = pf) {perror ("fopen"); return-1;} / / write the file fputc ('baked, pf); fputc (' fopen, pf); fputc ('tweeted, pf) / / close the file fclose (pf); pf = NULL; return 0;}
We write to the test.txt file, if we write to a file, if the file exists and has content, the write operation will destroy the contents of the file and rewrite what we want to write at present. Because we do not create such a file in our current folder, the program automatically creates it for us. We use the fputs function, which writes a character to the file. In this code, we write the characters "b", "I" and "t" respectively.
Fputs function
Int main () {FILE* pf = fopen ("test.txt", "w"); if (NULL = = pf) {perror ("fopen"); return-1;} / / write file / / write a line of data fputs ("hello world\ n", pf); fputs ("hello bit\ n", pf) / / close the file fclose (pf); pf = NULL;}
The fputs function outputs a string to the file. Because this file has been created before and has the character "bit", now that we re-write, the character "bit" will be destroyed and the two strings hello world and hello bit will be rewritten.
Fprintf function
Struct S {int n; double d;}; int main () {struct S s = {100,3.14}; FILE* pf = fopen ("data.txt", "w"); if (NULL = = pf) {perror ("fopen"); return-1 } / / write file fprintf (pf, "% d% lf", s.n, s.d); / / close file fclose (pf); pf = NULL;}
The fprintf function outputs formatting characters to the file pointed to by pf (the so-called format is similar to "% d% lf" in the code), so the program outputs "100s" and "3.140000" to the file.
Fscanf function
Struct S {int n; double d;}; int main () {struct S s = {0}; FILE* pf = fopen ("data.txt", "r"); if (NULL = = pf) {perror ("fopen"); return-1 } / / read the file fscanf (pf, "% d% lf", & (s.n), & (s.d)); printf ("% d% lf\ n", s.n, s.d); / / close the file fclose (pf); pf = NULL;}
The function of the fscanf function is to read the formatted data from the file and store it in memory, so there are only "3.140000" and "100" data in the file, so the program will read the two data into the structure variable s.
The use of the last two functions is similar to the fgetc fputc fgets fputs function, I will not give an example, students to try it!
The above is all the contents of the article "how to operate files in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.