6.6 Processing Characters
Sometimes, you may want to read the text from the file as characters instead of numerical data. The fscanf function, however, as indicated in the previous section, skips over white space characters when reading individual characters from the file. If you need to read all characters, including white space characters, then you must use the fgetc function instead of the fscanf function. The fgetc function takes the file variable as an argument and returns the next character in the file without skipping over any other characters
- int ch;
- ch = fgetc(infile);
This function is typically used within a loop to read multiple characters from a text file. In the following program, we read the individual characters from the text file and simply display them to the terminal.
Listing
Program: showfile.cc
|