In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "what is the sequential reading and writing function of C language files". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, the sequential read and write functions fgetc and fputc of files
Fgetc
The character input function applies to all input streams
Fputc
The character output function applies to all output streams
Int fgetc (FILE * stream); int fputc (int c, FILE * stream)
Fputc writes files
Int main () {FILE* pfWrite = fopen ("test.txt", "w"); if (pfWrite = = NULL) / / verify {printf ("% s\ n", strerror (errno)); / / if write file error printing error reason return 0;} / write file fputc ("a", pfWrite) Fputc ("b", pfWrite); fputc ("c", pfWrite); / / close the file fclose (pfWrite); pfWrite = = NULL; return 0;}
Fgetc reads the file
Int main () {FILE* pfRead = fopen ("test.txt", "r"); if (pfRead = = NULL) {printf ("% s\ n", strerror (errno)); return 0;} / read file printf ("% s", fgetc (pfRead)); / / a printf ("% s", fgetc (pfRead)) / b printf ("% s", fgetc (pfRead)); / / c / / close the file fclose (pfRead); pfRead = = NULL; return 0;} fgets and fputs
The fges text line input function applies to all input streams
The fputs text line input function applies to all output streams
The functions of the two functions:
/ / Get a string from a stream. Gets a string from the stream. Char * fgets (char * string, int n, FILE * stream); / / Write a string to a stream. Writes a string to the stream. Int fputs (const char * string, FILE * stream)
Read a line with the fgets function:
Int main () {char buff [100] = {0}; FILE* pf = fopen ("test.txt", "r"); if (pf = = NULL) {return 0;} / read file fgets (buff, 100, pf); printf ("% s", buff) / / notice that there is a newline character in buff (there is a newline character at the end of the file) / / opened successfully, read the file, and close the file fclose (pf); pf = = NULL; return 0;}
Read a line and print it to the monitor as shown in the picture:
Write a line with the fputs function:
Int main () {char buff [100] = {0}; FILE* pf = fopen ("test.txt", "w"); if (pf = = NULL) {return 0;} / / write file fputs ("hello\ n", pf); fputs ("word\ n", pf) / / Open successfully, write the file, close the file fclose (pf); pf = NULL; return 0;}
Write a line to the file with fputs: as follows:
Fgets and fputs can also operate keyboards and screens
Keyboard input abcd enter screen print abcd
Fscanf and fprintf
The fscanf formatting input function applies to all input streams
The fprintf formatted output function applies to all output streams
Simple comparison of scanf and fscanf,printf and fprintf, the usage is very similar
Int scanf (const char * format [, argument]... Int fscanf (FILE * stream, const char * format [, argument]... Int printf (const char * format [, argument]... ); int fprintf (FILE * stream, const char * format [, argument]...)
Fprintf writes files
Struct S {int n; float score; char arr [10];}; int main () {struct S s = {100,3.14, "abc"}; FILE* pf = fopen ("test.txt", "w"); if (pf = = NULL) {return 0 } / / formatted file fprintf (pf, "d% f% s", s.n, s.score, s.arr); fclose (pf); pf = NULL; return 0;}
Fscanf reads the file
Struct S {int n; float score; char arr [10];}; int main () {struct S s = {100,3.14, "abc"}; FILE* pf = fopen ("test.txt", "w"); if (pf = = NULL) {return 0 } / / formatted input data fscanf (pf, "d% f% s", s.n, s.score, s.arr); fclose (pf); pf = NULL; return 0;}
Of course, fscanf and fprintf functions can also be applied to standard input / output streams (stdin,stdout).
Compare a set of functions
Scanf/fscanf/sscanf
Ptintf/fprintf/sprintf
Scanf/printf: is a formatted input / output statement for standard input streams / standard output streams
Fscnaf/fprintf: is a formatted input / output statement for all input / output streams
Sscanf/sprintf:sscanf reads formatted data from a string. Sprintf is to output formatted data to (store to) strings
Fraed and fwriite
Fread binary input function applies to files
Fwrite binary output function applies to files
Size_t fwrite (const void * buffer, size_t size, size_t count, FILE* stream); size_t fread (void * buffer, size_t size, size_t count, FILE* stream); struct S {char name [20]; int age; double score;}; int main () {struct S s = {"Zhang San", 2055.6}; FILE* pf = fopen ("test.txt", "wb") If (pf = = NULL) {return 0;} / / binary file fwrite (& s, sizeof (struct S), 1, pf); fclose (pf); pf = NULL; return 0;}
Fread reads the file and fwrite writes the file. The usage of the two functions is similar
Second, the random read-write function of the file
The functions described above are all sequential read-write functions, and sometimes we need to start reading and writing functions from a certain location. The following functions perform their functions.
Fseek function
Locate the file pointer according to its location and offset
Moves the file pointer to a specified location.int fseek (FILE* stream, long offset, int origin); / / what the three parameters refer to: the current position of the pf offset file pointer int main () {FILE* pf = fopen ("test.txt", "r"); if (pf = = NULL) {return 0 } / / locate the file pointer fseek (pf,2,SEEK_CUR); / / offset is 2 / / read the file int ch = fgetc (pf); prinf ("% c\ n", ch); fclose (pf); pf = NULL; return 0;}
As shown in the figure, the offset is 2 from the current position an of the file pointer, and print c
SEEK_CUR
Current position of file pointer (current location of the file pointer)
SEEK_END (location at the end of the file)
End of file
SEEK_SET (file start location)
Beginning of file
III. Judgment on the end of the document
We know: EOF-- > the flag for the end of the end of file file
If there is nothing in a file, what we open the file to read is "- 1", and there is an EOF at the end of the file.
Feof function
Keep in mind that during the file reading process, the return value of the feof function cannot be used to determine whether the file is finished or not.
Instead, it is used to determine whether the read fails to end when the file is finished, or when the tail EOF is encountered to end the file.
Whether the text file is finished reading, and determine whether the return value is EOF or NULL
For example, fgetc determines whether it is EOF fgets and whether the return value is NULL.
Judge the end of reading the binary file and determine whether the return value is less than the actual number to be read.
For example, fread determines whether the returned value is less than the actual number to be read.
This is the end of the content of "what are the sequential reading and writing functions of C language files". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.