MCS-201 · Unit 8

File Handling

Section 4 of 8

Block 2 · Unit 8File Handling10 min · Section 4 of 8

8.4String Input and Output

Covers IGNOU MCS-201 §8.3.2

Reading a file one character at a time is slow to write when you really want whole lines. fgets and fputs read and write strings, normally one line at a time.

fgets

Prototype
char *fgets(char *str, int n, FILE *fp);

fgets reads characters into str until one of three things happens: it has read n - 1 characters, it has read a newline (which is stored in str), or the file ends. It then adds '\0'. It returns str on success, or NULL if the end of file was reached before any character was read (or an error occurred).

Since fgets never writes more than n bytes, it is safe against overflowing the array, which is why it replaces the unsafe gets (removed from C in 2011).

str size nLine in fileWhat fgets stores
80Hello\n"Hello\n"
80Hello at the very end, no newline"Hello"
4Hello\n"Hel" (the rest is read by the next call)

fputs

Prototype
int fputs(const char *str, FILE *fp);

fputs writes the string without its '\0' and, unlike puts, does not add a newline. It returns a non-negative value on success and EOF on error.

Key takeaways

  • fgets reads up to n - 1 characters, stopping after a newline, and always adds '\0'.
  • It returns NULL at end of file; loop on that.
  • fputs writes a string and adds no newline.
  • A blank line is still a line ("\n").

Practice questions

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

Question 1 · multiple choice

What does fgets return when there is nothing more to read?

Show answer

Answer: C. NULL

fgets returns a pointer: str on success and the null pointer at end of file or on error.

Question 2 · multiple choice

With char s[6];, what does fgets(s, 6, fp) store if the next line in the file is Computer\n?

Show answer

Answer: C. "Compu"

At most 6 - 1 = 5 characters are read, then '\0' is added; the rest of the line is left for the next call.

Question 3 · multiple choice

What is the difference between fputs(s, stdout) and puts(s)?

Show answer

Answer: C. puts adds a newline; fputs does not

puts always appends '\n'; fputs writes exactly the characters of the string.

Question 4 · multiple choice

A file contains the two lines ab and cd, each ending with a newline. What does this program print?

C · two_lines.c
#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE *fp = fopen("two.txt", "r");
    char buf[20];
    size_t total = 0;
    if (fp == NULL)
        return 1;
    while (fgets(buf, sizeof buf, fp) != NULL)
        total += strlen(buf);
    fclose(fp);
    printf("%zu\n", total);
    return 0;
}
Show answer

Answer: B. 6

Each buf holds two letters plus the newline, so strlen is 3 for each line; 3 + 3 = 6.

Question 5 · multiple choice

Why is fgets preferred over gets?

Show answer

Answer: A. It takes the buffer size and never writes past it

gets has no size limit, so long input overflows the array; it was removed from the C standard in C11.

Question 6 · short answer

A program counts lines with while (!feof(fp)) { fgets(s, 80, fp); n++; }. Explain why the count can be wrong and give the corrected loop.

Show worked answer

feof becomes true only after a read has failed at the end of the file. After the last real line is read, feof is still false, so the loop runs once more: fgets fails (returns NULL), but n is still increased, giving one line too many. The corrected loop tests the read itself: while (fgets(s, 80, fp) != NULL) n++;.

↑ ↓ move↵ openTab switch type