The fgets and fputs functions are explained in C language file operation

  • 2020-05-24 05:56:13
  • OfStack

The fgets and fputs functions are explained in detail in the operation of C language files

First presents api


fgets 
 grammar : 
 #include <stdio.h>
 char *fgets( char *str, int num, FILE *stream );

The function fgets() reads [num-1] characters from the given file stream and dumps them into str(string). fgets() stops at the end of the line, in which case str(string) is terminated by a new line character. str(string) will end with null.fgets() returns str(string) on success and NULL on failure.

They understand

1. Read 1 line at a time, if the number of 1 line characters read is len > bufsize-1, buf[bufsize-1] = '0'

The file handle points to the location of bufsize.

2. Read 1 line at a time. If the number of 1 line characters read is len = bufsize-1, then buf[bufsize-1] = '0'

The file handle points to the location of bufsize.

3. Read 1 line at a time, if the number of 1 line characters read is len < bufsize-1, then buf [len-1] = '\n', buf [len] = '\0'

The file handle points to the beginning of the next line.

Verification code:


int i=0;
 FILE *fp;
 char buff[5];
 // Assign the array to 9 For later comparison with the assignment  
 memset(buff,9,sizeof(buff));
 
 char *fName = "d:/123.txt"; 
 fp = fopen(fName,"r");
 fgets(buff,sizeof(buff),fp);
 
 // Print out the array ascii code  
 printf("buff Array contents: \n");
 for(i=0;i<sizeof(buff);i++)
 {
 printf("%d ",(int)buff[i]); 
 }
 printf("\n");
 
 // Keep getting it 1 Line character to verify the position of the file handle 
 memset(buff,9,sizeof(buff));
 fgets(buff,sizeof(buff),fp);
 // Print out the array ascii code  
 printf(" Under the 1 line buff Array contents: \n");
 for(i=0;i<sizeof(buff);i++)
 {
 printf("%d ",(int)buff[i]); 
 }
 printf("\n");
 
 fclose(fp);

123. Content of txt:


1234
asdf

Output results:

buff length is 4, verify understanding 1

buff array contents:


49 50 51 0
 Under the 1 line buff Array contents: 
52 10 0 9
 

buff length is 5, verify understanding 2


buff Array contents: 
49 50 51 52 0
 Under the 1 line buff Array contents: 
10 0 9 9 9

buff length is 10, verify understanding 3

Contents of buff array:


49 50 51 52 10 0 9 9 9 9
 Under the 1 line buff Array contents: 
97 115 100 102 0 9 9 9 9 9

File write data essence:

Insert the string into the file and insert the newline '\n' at the end of the string

fgets function essence:

Write buffsize-1 characters from the file to the cache space and '\0' characters to the buffsize location of the cache space according to the size of the cache space to be written.

fputs function essence:

Writes the specified string to the file stream without the string ending '\0', which replaces the data in the file stream but does not add data.

The validation code


  FILE *fp;
 char *fName = "d:/123.txt"; 
 fp = fopen(fName,"r+");
 fseek(fp,3,SEEK_CUR);
 fputs("Insert Strings",fp);
 fclose(fp);

Original file content:


1234
Second Line. Second Strings.

Contents of the modified file:


123Insert Strings. Second Strings.

As can be seen, replace "\n and Second Line." with "Insert Strings."

fputc function essence:

The function fputc() writes the given character ch to the specified location in the given output stream. The return value is the character, and in the case of an error, EOF.

fgetc function essence:

The fgetc() function returns the next character from the stream(stream), and if it reaches the end of the file or if an error occurs, returns whatever character EOF is (including '\0','\n').

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: