C language fgetc and fputc function usage details

Posted by bigbstanley on Fri, 24 Sep 2021 16:08:14 +0200

When reading and writing a file as characters, you can read one character from the file or write one character to the file at a time. Two functions are mainly used: fgetc() and fputc().

Character reading function fgetc

Fgetc is the abbreviation of file get char, which means to read a character from the specified file. The usage of fgetc() is:

int fgetc (FILE *fp);

  
  • 1

fp is the file pointer. fgetc() returns the read characters when reading is successful, and EOF when reading to the end of the file or reading fails.

EOF is the abbreviation of end of file, which means the end of the file. It is a macro defined in stdio.h. its value is a negative number, often - 1. The reason why the return value type of fgetc() is int is to accommodate this negative number (char cannot be negative).
EOF is not absolutely - 1, but can also be other negative numbers, depending on the implementation of the compiler.
Usage example of fgetc():

char ch;
FILE *fp = fopen("D:\\demo.txt", "r+");
ch = fgetc(fp);

  
  • 1
  • 2
  • 3

Means to read a character from the D:\demo.txt file and save it to the variable ch.

There is a position pointer inside the file, which is used to point to the current read and write position, that is, the first few bytes to read and write. When a file is opened, the pointer always points to the first byte of the file. After using the fgetc() function, the pointer moves back one byte, so you can use fgetc() to read multiple characters multiple times in a row.

Note: the position pointer inside this file is not the same as the pointer in C language. The position pointer is just a flag, indicating the position of the file read and write, that is, the first few bytes read and write. It does not represent the address. Every time the file is read and written, the position pointer will move once. It does not need you to define and assign values in the program, but is automatically set by the system and hidden from the user.

[example] display the contents of D:\demo.txt file on the screen.

#include<stdio.h>
int main(){
    FILE *fp;
    char ch;
<span class="token comment">//If the file does not exist, prompt and exit</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>fp<span class="token operator">=</span><span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string">"D:\\demo.txt"</span><span class="token punctuation">,</span><span class="token string">"rt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"Fail to open file!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//Read one byte at a time until the reading is complete</span>
<span class="token keyword">while</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>ch<span class="token operator">=</span><span class="token function">fgetc</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">EOF</span> <span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">putchar</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">putchar</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//Output newline</span>

<span class="token function">fclose</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Create a demo.txt file under disk D, enter any content and save it, run the program, and you will see that all the contents just entered are displayed on the screen.

The function of the program is to read characters one by one from the file and display them on the screen until reading is completed.

Line 13 of the program is the key. The condition of the while loop is (ch = fgetc (FP))= EOF. fget() reads one character from the position of the position pointer at a time and saves it to the variable ch. the position pointer moves back one byte. When the file pointer moves to the end of the file, fget () cannot read characters, so it returns EOF, indicating that the file reading is over.

Description of EOF

EOF originally means the end of the file, which means the end of reading, but many functions also return EOF when reading errors. When EOF is returned, is the file read completed or read errors? We can judge by two functions in stdio.h, feof() and ferror().

feof() function is used to judge whether the internal pointer of the file points to the end of the file. Its prototype is:

int feof ( FILE * fp );

 
  • 1

Returns a non-zero value when pointing to the end of the file, otherwise returns a zero value.

The ferror() function is used to judge whether the file operation is wrong. Its prototype is:

int ferror ( FILE *fp );

 
  • 1

Returns a non-zero value when an error occurs, otherwise it returns a zero value.

It should be noted that file errors are very rare. The above example can basically ensure that the data in the file is read. If you pursue perfection, you can also add judgment and give tips:

#include<stdio.h>
int main(){
    FILE *fp;
    char ch;
<span class="token comment">//If the file does not exist, prompt and exit</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>fp<span class="token operator">=</span><span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string">"D:\\demo.txt"</span><span class="token punctuation">,</span><span class="token string">"rt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"Fail to open file!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//Read one byte at a time until the reading is complete</span>
<span class="token keyword">while</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>ch<span class="token operator">=</span><span class="token function">fgetc</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">EOF</span> <span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">putchar</span><span class="token punctuation">(</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">putchar</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//Output newline</span>

<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">ferror</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"Read error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"Read successful"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">fclose</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

In this way, you can know well whether it is an error or a normal read.

Character writing function fputc

Fputc is the of file output char, so it means to write a character to the specified file. The usage of fputc() is:

int fputc ( int ch, FILE *fp );

 
  • 1

ch is the character to be written and fp is the file pointer. fputc() returns the written character upon successful writing and EOF upon failure. The return value type is int to accommodate this negative number. For example:

fputc('a', fp);

 
  • 1

Or:

char ch = 'a';
fputc(ch, fp);

 
  • 1
  • 2

Indicates that the character 'a' is written to the file pointed to by fp.
Two points
1. The written file can be opened in write, read-write and append mode. When an existing file is opened in write or read-write mode, the original file content will be cleared and the written characters will be placed at the beginning of the file. If you want to keep the original file content and put the written characters at the end of the file, you must open the file by appending. No matter how it is opened, the written file is created if it does not exist.

2. Each time a character is written, the internal position pointer of the file moves backward by one byte.

Example: input a line of characters from the keyboard and write to the file.

#include<stdio.h>
int main(){
    FILE *fp;
    char ch;
<span class="token comment">//Judge whether the file is successfully opened</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>fp<span class="token operator">=</span><span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string">"D:\\demo.txt"</span><span class="token punctuation">,</span><span class="token string">"wt+"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span> <span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">puts</span><span class="token punctuation">(</span><span class="token string">"Fail to open file!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Input a string:\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//Read one character at a time from the keyboard and write to the file</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span> <span class="token punctuation">(</span>ch<span class="token operator">=</span><span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token string">'\n'</span> <span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token function">fputc</span><span class="token punctuation">(</span>ch<span class="token punctuation">,</span>fp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">fclose</span><span class="token punctuation">(</span>fp<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Run the program, enter a line of characters and press enter to end. Open the demo.txt file under disk D to see the content just entered.

The program reads one character from the keyboard each time and writes it to the file until the Enter key is pressed, the while condition is not established, and the reading is ended.

Topics: C C++