File I/O Questions with Answers

28 practice questions on File I/O (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 28 free in the AptiNest app.

Sample questions

Question 1Easy

Apart from releasing the stream, what does fclose do for a file opened for writing?

  1. Deletes the file if it is empty
  2. Flushes any buffered output to the file
  3. Converts the file to binary mode
  4. Rewinds the file to the beginning

Answer: B. Flushes any buffered output to the file

Explanation

Output is usually buffered; fclose writes out the remaining buffer before closing. Forgetting it can lose data.

Step-by-step solution

  1. Fclose ensures all data in memory is written to the file.
  2. Buffered output might not be saved until explicitly flushed.
  3. Flushing writes remaining buffered data to the disk.
  4. This prevents data loss when closing the file.
  5. So, fclose flushes any buffered output to the file.

Why the other options are wrong

  • A. Fclose does not delete the file, even if it is empty; the file stays on disk.
  • C. Fclose does not change text or binary mode; the mode is chosen when the file is opened.
  • D. Fclose does not rewind the file; the stream is closed and no position is kept.

Tip: Always use fclose after writing to ensure data is saved.

Question 2Easy

By convention, which standard stream should a C program use for error messages?

  1. stdout
  2. stderr
  3. stdlog
  4. stdin

Answer: B. stderr

Explanation

stderr is separate from stdout, so errors still show when normal output is redirected to a file.

Step-by-step solution

  1. C programs use stderr for error messages by convention.
  2. Stderr is separate from stdout, ensuring error messages are always visible.
  3. Redirecting stdout does not affect stderr output.
  4. This separation helps in debugging and logging errors effectively.
  5. Thus, the correct standard stream for error messages is stderr.

Why the other options are wrong

  • A. Stdout is for normal program output; error messages go to stderr so they stay separate.
  • C. Stdlog is not a standard C stream; C defines only stdin, stdout and stderr.
  • D. Stdin is the input stream, used for reading data, not for writing error messages.

Tip: Remember: stderr is for errors, stdout is for regular output.

Question 3Easy

What is EOF in C?

  1. The value 0 returned by fgets
  2. A constant of type char equal to 255
  3. A negative int constant returned by input functions at end of file or on error
  4. A special character stored at the end of every file

Answer: C. A negative int constant returned by input functions at end of file or on error

Explanation

EOF is a macro in <stdio.h> for a negative int (commonly -1). Files do not contain an EOF character.

Step-by-step solution

  1. EOF is a macro in stdio.h that stands for an int constant, usually -1.
  2. Input functions return EOF when the end of file is reached or a read error occurs.
  3. EOF is not a character stored in files and is not 0 or 255.
  4. So the correct answer is option C, a negative int constant.

Why the other options are wrong

  • A. Fgets returns NULL at end of file, not 0 for EOF.
  • B. EOF is an int, not a char with the value 255.
  • D. Files do not contain an EOF character; the end is found from the read result.

Tip: Store the return value of getchar or fgetc in an int, then compare it with EOF.

Question 4Easy

What does fopen return when it cannot open the file?

  1. EOF
  2. -1
  3. NULL
  4. A FILE pointer with an error flag set

Answer: C. NULL

Explanation

fopen returns a null pointer on failure (and usually sets errno), so always check the result before using it.

Step-by-step solution

  1. fopen returns NULL on failure.

Why the other options are wrong

  • A. EOF is for reads.
  • B. -1 is for other functions.
  • D. No such return.

Tip: NULL means failure.

Question 5Medium

What is the output?

#include <stdio.h>

int main(void) {
    int a[4] = {5, 6, 7, 8}, b[10] = {0};
    FILE *f = fopen("d.bin", "wb");
    if (!f) return 1;
    fwrite(a, sizeof a[0], 4, f);
    fclose(f);

    f = fopen("d.bin", "rb");
    if (!f) return 1;
    size_t n = fread(b, sizeof b[0], 10, f);
    printf("%zu %d\n", n, b[3]);
    fclose(f);
    remove("d.bin");
    return 0;
}
  1. 16 8
  2. 10 8
  3. 4 8
  4. 4 0

Answer: C. 4 8

Explanation

fread returns the number of complete items read, not bytes. Only 4 ints are in the file, so it returns 4 and b[3] is 8.

Step-by-step solution

  1. Fwrite writes 4 ints of 4 bytes each, so the file holds 16 bytes.
  2. Fread asks for up to 10 items of 4 bytes; only 4 items exist, so it returns 4.
  3. b[3] holds the fourth int written, which is 8. The output is 4 8, the marked option C.

Why the other options are wrong

  • A. 16 is the byte count; fread returns the number of items read, not bytes.
  • B. 10 is the number asked for, but only 4 items were in the file.
  • D. Fread fills b[3] with 8, so it is not 0; the count 4 is correct.

Tip: Fread and fwrite return item counts; multiply by the item size to get bytes.

Question 6Medium

What is the difference between opening a file with "r" and with "rb"?

  1. "rb" opens the file read-only, while "r" allows writing too
  2. Text mode may translate line endings (e.g. CRLF on Windows); binary mode reads bytes unchanged
  3. "r" reads ASCII only; "rb" reads Unicode
  4. There is no difference on any platform

Answer: B. Text mode may translate line endings (e.g. CRLF on Windows); binary mode reads bytes unchanged

Explanation

On POSIX systems they behave the same, but on Windows text mode converts "\r\n" to "\n", so binary data should use "rb".

Step-by-step solution

  1. Text mode may translate line endings; binary mode does not.

Why the other options are wrong

  • A. Both are read-only.
  • C. Neither is about Unicode.
  • D. Differs on some platforms.

Tip: Binary = bytes unchanged.

Question 7Medium

What is the output?

#include <stdio.h>
#include <string.h>

int main(void) {
    FILE *f = fopen("t.txt", "w+");
    if (!f) return 1;
    fprintf(f, "%5d|%-4s|", 42, "ab");
    rewind(f);
    char buf[32];
    if (fgets(buf, sizeof buf, f))
        printf("[%s] %zu\n", buf, strlen(buf));
    fclose(f);
    remove("t.txt");
    return 0;
}
  1. [ 42| ab|] 11
  2. [42|ab|] 6
  3. [42 |ab |] 11
  4. [ 42|ab |] 11

Answer: D. [ 42|ab |] 11

Explanation

%5d right-aligns 42 in 5 columns, and %-4s left-aligns "ab" in 4 columns, so the line is 5 + 1 + 4 + 1 = 11 characters.

Step-by-step solution

  1. fprintf writes %5d as 42 right-aligned in 5 columns: three spaces then 42.
  2. %-4s writes ab left-aligned in 4 columns: ab then two spaces.
  3. The file holds the text spaces-42, a bar, ab and spaces, a bar: 5 + 1 + 4 + 1 = 11 characters.
  4. rewind goes back to the start and fgets reads the whole line, so strlen is 11.
  5. So the output is a right-aligned 42, then ab left-aligned, with length 11.

Why the other options are wrong

  • A. Ab is left-aligned, so the spaces come after it, not before it.
  • B. The padding spaces are kept, so the length is 11, not 6.
  • C. 42 is right-aligned, so its spaces come before it.

Tip: A minus sign in the format means left-align; no minus means right-align.

Question 8Medium

What is the output?

#include <stdio.h>

int main(void) {
    char buf[16];
    FILE *f = fopen("t.txt", "w");
    if (!f) return 1;
    fputs("ab", f);
    fclose(f);

    f = fopen("t.txt", "a");
    if (!f) return 1;
    fputs("cd", f);
    fclose(f);

    f = fopen("t.txt", "r");
    if (!f) return 1;
    if (fgets(buf, sizeof buf, f)) printf("%s ", buf);
    fclose(f);

    f = fopen("t.txt", "w");
    if (!f) return 1;
    fputs("x", f);
    fclose(f);

    f = fopen("t.txt", "r");
    if (!f) return 1;
    if (fgets(buf, sizeof buf, f)) printf("%s\n", buf);
    fclose(f);
    remove("t.txt");
    return 0;
}
  1. abcd x
  2. abcd abcdx
  3. cd x
  4. ab x

Answer: A. abcd x

Explanation

"a" appends, so the file becomes "abcd". Reopening with "w" truncates it, leaving only "x".

Step-by-step solution

  1. The first write creates ab; the append mode adds cd, so the file holds abcd.
  2. Fgets reads abcd, which has no newline, and printf shows abcd followed by a space.
  3. Reopening with w empties the file and writes x; fgets reads x and prints it. The output is abcd x, the marked option A.

Why the other options are wrong

  • B. The second read starts from the emptied file, which holds only x, so it is not abcdx.
  • C. The first read includes ab as well, so it shows abcd, not cd.
  • D. The append added cd, so the first read is abcd, not ab.

Tip: Mode w empties a file on open; mode a adds to the end and keeps the old bytes.

Question 9Hard

What is the output?

#include <stdio.h>

int main(void) {
    FILE *f = fopen("t.txt", "w");
    if (!f) return 1;
    fputs("abc", f);
    fclose(f);

    f = fopen("t.txt", "r");
    if (!f) return 1;
    int n = 0;
    while (!feof(f)) {
        fgetc(f);
        n++;
    }
    printf("%d\n", n);
    fclose(f);
    remove("t.txt");
    return 0;
}
  1. 2
  2. 3
  3. 4
  4. The loop never ends

Answer: C. 4

Explanation

feof becomes true only after a read has already failed, so the loop body runs a 4th time for the fgetc that returns EOF.

Step-by-step solution

  1. The loop checks feof before reading.
  2. Three reads get a, b, c; EOF flag is not set yet.
  3. The fourth read hits EOF, n = 4.

Why the other options are wrong

  • A. Two does not read all characters.
  • B. EOF is detected only after a failed read.
  • D. The loop ends.

Tip: Test the return value of fgetc.

Question 10Hard

Why is this loop a bug?

char c;
while ((c = fgetc(fp)) != EOF)
    putchar(c);
  1. fgetc returns a char *, not a character
  2. putchar cannot print a char variable
  3. EOF is only returned by fgets, not fgetc
  4. fgetc returns int; storing it in char can make a real byte compare equal to EOF, or EOF never match

Answer: D. fgetc returns int; storing it in char can make a real byte compare equal to EOF, or EOF never match

Explanation

fgetc returns an unsigned char value or EOF as an int. If char is signed, byte 0xFF looks like EOF; if char is unsigned, EOF is never seen and the loop never ends.

Step-by-step solution

  1. Fgetc returns int, but char is stored in c.
  2. If char is signed, 0xFF looks like EOF, causing false match.
  3. If char is unsigned, EOF is never seen, loop runs forever.
  4. This makes the loop fail to detect EOF correctly.
  5. Thus, fgetc returns int; storing it in char can make a real byte compare equal to EOF, or EOF never match.

Why the other options are wrong

  • A. Fgetc returns an int, not a char pointer; a char pointer would be a type error in this loop.
  • B. Putchar can print a char value without problem; the bug is in the EOF check, not in printing.
  • C. EOF is returned by fgetc too; the value -1 is what fgetc returns at end of file or on error.

Tip: Always use int for fgetc results to avoid EOF detection issues.

Practise all 28 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics