MCS-201 · Unit 8

File Handling

Section 3 of 8

Block 2 · Unit 8File Handling13 min · Section 3 of 8

8.3Character Input and Output

Covers IGNOU MCS-201 §8.3, 8.3.1

The simplest way to process a file is one character at a time. This section introduces the character functions, explains the special value EOF, and shows the correct loop for reading to the end of a file.

The functions

FunctionPrototypeReturns
fgetcint fgetc(FILE *fp);The next character as an unsigned char converted to int, or EOF at end of file or on error
getcint getc(FILE *fp);Same as fgetc
fputcint fputc(int ch, FILE *fp);The character written, or EOF on error
putcint putc(int ch, FILE *fp);Same as fputc

getc and putc behave exactly like fgetc and fputc; the only difference is that the library is allowed to implement getc and putc as macros, which can be slightly faster.

What EOF is

EOF is not a character stored in the file. Modern operating systems record the length of each file, and the library returns EOF when you try to read past that length.

Because EOF must be different from every possible character, the functions return int, not char. Always store their result in an int variable.

Reading to the end correctly

feof(fp) returns a non-zero value (true) after a read has hit the end of the file, and 0 otherwise. ferror(fp) returns non-zero if an error occurred. They are useful after a loop ends, to find out why it ended.

Counting characters, words and lines

One of the unit’s objectives is to count the words in a file. A word starts whenever a non-space character follows a space (or the start of the file).

Key takeaways

  • getc/fgetc read one character; putc/fputc write one.
  • They return int so that EOF (a negative value) can be told apart from every character.
  • Test the read result for EOF; use feof and ferror only after the loop.
  • Files contain no end-of-file marker.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Why is the variable that receives getc’s result declared as int?

Show answer

Answer: C. So that it can hold every character value and also EOF

EOF is a negative value distinct from all 256 byte values; a char cannot represent all 257 possibilities.

Question 2 · multiple choice

What does feof(fp) tell you?

Show answer

Answer: B. Whether an earlier read has already reached the end of the file

The end-of-file indicator is set only when a read tries to go past the end, so feof looks backwards, not forwards.

Question 3 · multiple choice

A file contains exactly AB (2 bytes). How many times does the body of while (!feof(fp)) { ch = getc(fp); putc(ch, out); } run?

Show answer

Answer: C. 3

The first two reads return ‘A’ and ‘B’; feof is still false, so a third read happens, returns EOF, and that value is written as an extra byte.

Question 4 · multiple choice

What does this program print if one.txt contains the 3 characters x, y, z and no newline?

C · count_chars.c
#include <stdio.h>

int main(void)
{
    FILE *fp = fopen("one.txt", "r");
    int ch, n = 0;
    if (fp == NULL)
        return 1;
    while ((ch = fgetc(fp)) != EOF)
        n++;
    fclose(fp);
    printf("%d\n", n);
    return 0;
}
Show answer

Answer: A. 3

The loop counts each character successfully read and stops as soon as fgetc returns EOF, so the count is exactly 3.

Question 5 · multiple choice

Which statement about getc and fgetc is correct?

Show answer

Answer: D. They behave the same, but getc may be implemented as a macro

Both read one character from any stream and return EOF at the end; the only difference is the permission to implement getc as a macro.

Question 6 · short answer

Write a loop that displays a text file on the screen character by character, and say what is printed if the loop is written with feof instead.

Show worked answer

int ch; while ((ch = getc(fp)) != EOF) putchar(ch);. Each character is read and printed, and the loop ends as soon as getc reports the end. If the loop is written as while (!feof(fp)) { ch = getc(fp); putchar(ch); }, the final failed read still runs the body, so putchar(EOF) prints one extra garbage character (byte 255) after the file’s contents.

↑ ↓ move↵ openTab switch type