MCS-201 · Unit 8

File Handling

Section 5 of 8

Block 2 · Unit 8File Handling11 min · Section 5 of 8

8.5Formatted Input and Output

Covers IGNOU MCS-201 §8.3.3

When a file holds numbers, names and other values in a fixed layout, it is convenient to read and write them exactly as with scanf and printf. That is what fscanf and fprintf do.

fprintf and fscanf

Prototypes
int fprintf(FILE *fp, const char *format, ...);
int fscanf(FILE *fp, const char *format, ...);

They work exactly like printf and scanf, with a file pointer as the first argument. In fact printf(...) is fprintf(stdout, ...) and scanf(...) is fscanf(stdin, ...).

FunctionReturns
fprintfThe number of characters written, or a negative value on error
fscanfThe number of input items successfully assigned, or EOF if the input ends before the first item

The return value of fscanf is the key to reading a file of records: if each record has three fields, keep reading while fscanf returns 3.

Check Your Progress 2, question 1

This exercise writes numbers to two files with fprintf and reads them back with fscanf.

Key takeaways

  • fprintf(fp, ...) and fscanf(fp, ...) work like printf and scanf on a file.
  • fprintf returns characters written; fscanf returns items assigned or EOF.
  • Loop while fscanf returns the number of fields in a record.
  • Use widths such as %29s to protect arrays.

Practice questions

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

Question 1 · multiple choice

What does fscanf(fp, "%d %d", &x, &y) return when both numbers are read successfully?

Show answer

Answer: C. 2

fscanf returns the number of items assigned, here two, regardless of how many characters were consumed.

Question 2 · multiple choice

Which call writes the value of n to the file fp?

Show answer

Answer: D. fprintf(fp, "%d", n);

The file pointer is always the first argument of fprintf, before the format string.

Question 3 · multiple choice

What does fscanf return if it reaches the end of the file before reading any item?

Show answer

Answer: A. EOF

EOF (a negative value) signals that input ended before the first conversion. 0 means data was there but did not match the format.

Question 4 · multiple choice

A file nums.txt contains 4 7 9 followed by a newline. What does this program print?

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

int main(void)
{
    FILE *fp = fopen("nums.txt", "r");
    int n, sum = 0;
    if (fp == NULL)
        return 1;
    while (fscanf(fp, "%d", &n) == 1)
        sum += n;
    fclose(fp);
    printf("%d\n", sum);
    return 0;
}
Show answer

Answer: C. 20

The loop reads 4, 7 and 9, then fscanf returns EOF and the loop stops; 4 + 7 + 9 = 20, with no number counted twice.

Question 5 · multiple choice

What does fprintf(fp, "%d,%d\n", 12, 345) return on success?

Show answer

Answer: B. 7

It writes “12,345” and a newline: 2 + 1 + 3 + 1 = 7 characters.

Question 6 · short answer

Write a program fragment that appends a new client record (account 106, name Priya, balance 2500) to bank.txt in the same format as the existing records.

Show worked answer

FILE *fp = fopen("bank.txt", "a"); if (fp == NULL) { perror("bank.txt"); return 1; } fprintf(fp, "%d %s %.0f\n", 106, "Priya", 2500.0); fclose(fp);. Mode "a" keeps the existing five records and writes the new line at the end; fprintf produces “106 Priya 2500” followed by a newline, which the reading program can process with the same fscanf format.

↑ ↓ move↵ openTab switch type