Bird
Raised Fist0
Cprogramming~20 mins

Reading from files in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C code reading a file?

Consider a file named data.txt containing the text "Hello\nWorld". What will this program print?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");
    char buffer[20];
    if (fp == NULL) {
        printf("Error opening file\n");
        return 1;
    }
    fgets(buffer, sizeof(buffer), fp);
    printf("%s", buffer);
    fclose(fp);
    return 0;
}
A
Hello
World
B
World
C
Hello
D
Error opening file
Attempts:
2 left
💡 Hint

Remember that fgets reads one line or up to the buffer size.

❓ Predict Output
intermediate
2:00remaining
What is the output of this code reading integers from a file?

Given a file numbers.txt with content:

10 20 30

What does this program print?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("numbers.txt", "r");
    int a, b, c;
    if (fp == NULL) {
        printf("Cannot open file\n");
        return 1;
    }
    fscanf(fp, "%d %d %d", &a, &b, &c);
    printf("%d %d %d\n", a, b, c);
    fclose(fp);
    return 0;
}
A
10 20 30
B
10 20
C
Cannot open file
D
30 20 10
Attempts:
2 left
💡 Hint

fscanf reads formatted input from the file.

❓ Predict Output
advanced
2:00remaining
What error does this code raise when reading a file?

What error will this program produce when run if missing.txt does not exist?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("missing.txt", "r");
    char c = fgetc(fp);
    printf("%c\n", c);
    fclose(fp);
    return 0;
}
ASegmentation fault
BPrints a random character
CCompilation error
DPrints EOF
Attempts:
2 left
💡 Hint

Check what happens if fopen returns NULL and you use the pointer without checking.

🧠 Conceptual
advanced
2:00remaining
How many characters are read by this code?

Given a file text.txt containing "abcde", how many characters will be read and printed by this program?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("text.txt", "r");
    int count = 0;
    int c;
    if (fp == NULL) return 1;
    while ((c = fgetc(fp)) != EOF) {
        putchar(c);
        count++;
    }
    fclose(fp);
    printf("\n%d\n", count);
    return 0;
}
A6
B5
C4
D0
Attempts:
2 left
💡 Hint

fgetc reads one character at a time until EOF.

🔧 Debug
expert
3:00remaining
What is the value of variable 'line' after running this code?

Assume the file input.txt contains exactly 1234567890.

This program reads a line from input.txt. What will be the value of line after fgets?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("input.txt", "r");
    char line[10];
    if (fp == NULL) return 1;
    fgets(line, 10, fp);
    fclose(fp);
    printf("%s", line);
    return 0;
}
A"1234567890"
B"12345678"
C"123456789\n"
D"123456789"
Attempts:
2 left
💡 Hint

Remember fgets reads up to size-1 characters and adds a null terminator.

Practice

(1/5)
1. What does the fopen() function with mode "r" do in C?
easy
A. It deletes a file.
B. It opens a file for reading only.
C. It creates a new file.
D. It opens a file for writing only.

Solution

  1. Step 1: Understand fopen modes

    The mode "r" in fopen means open the file for reading only.
  2. Step 2: Check other modes

    Modes like "w" are for writing, "a" for appending, and none delete files.
  3. Final Answer:

    It opens a file for reading only. -> Option B
  4. Quick Check:

    fopen with "r" = open for reading [OK]
Hint: Remember: "r" means read only mode in fopen [OK]
Common Mistakes:
  • Confusing "r" with write mode "w"
  • Thinking fopen deletes or creates files with "r"
  • Not checking if file exists before reading
2. Which of the following is the correct way to open a file named data.txt for reading in C?
easy
A. FILE *fp = fopen("data.txt", "r");
B. FILE *fp = fopen("data.txt", "w");
C. FILE *fp = fopen("data.txt", "a");
D. FILE *fp = fopen("data.txt");

Solution

  1. Step 1: Check fopen syntax

    fopen requires two arguments: filename and mode as strings.
  2. Step 2: Identify correct mode for reading

    "r" is the mode to open a file for reading.
  3. Final Answer:

    FILE *fp = fopen("data.txt", "r"); -> Option A
  4. Quick Check:

    Correct fopen syntax with "r" mode = FILE *fp = fopen("data.txt", "r"); [OK]
Hint: Always use two arguments in fopen: filename and mode [OK]
Common Mistakes:
  • Using "w" mode which opens for writing, not reading
  • Omitting the mode argument in fopen
  • Using "a" mode which opens for appending
3. What will be the output of the following C code if the file input.txt contains the line "Hello"?
FILE *fp = fopen("input.txt", "r");
char buffer[10];
fgets(buffer, 10, fp);
printf("%s", buffer);
fclose(fp);
medium
A. Hello\n
B. Hello
C. Hello\0
D. Error: file not found

Solution

  1. Step 1: Understand fgets behavior

    fgets reads a line including the newline character if it fits in the buffer.
  2. Step 2: Check file content and output

    The file has "Hello" followed by a newline, so fgets reads "Hello\n" and prints it.
  3. Final Answer:

    Hello\n -> Option A
  4. Quick Check:

    fgets includes newline if present = Hello\n [OK]
Hint: fgets reads newline character if space allows [OK]
Common Mistakes:
  • Assuming fgets removes newline automatically
  • Confusing null terminator with newline
  • Not checking if file opened successfully
4. Identify the error in this code snippet that reads a character from a file:
FILE *fp = fopen("file.txt", "r");
char c;
c = fgetc(fp);
printf("%c", c);
fclose(fp);
medium
A. fclose should be called before reading.
B. fgetc should be replaced with fgets.
C. Variable c should be an int, not char.
D. Missing check if fopen returned NULL before reading.

Solution

  1. Step 1: Check file open validation

    Always check if fopen returns NULL before reading to avoid errors.
  2. Step 2: Review other parts

    fgetc is correct for reading one char; c as char is acceptable; fclose after reading is correct.
  3. Final Answer:

    Missing check if fopen returned NULL before reading. -> Option D
  4. Quick Check:

    Always check fopen result before file operations [OK]
Hint: Check fopen != NULL before reading file [OK]
Common Mistakes:
  • Skipping NULL check after fopen
  • Confusing fgetc and fgets usage
  • Closing file too early
5. You want to read all lines from a file log.txt and print them. Which code snippet correctly does this in C?
hard
A. FILE *fp = fopen("log.txt", "r"); char c; while((c = fgetc(fp)) != EOF) { printf("%c", c); } fclose(fp);
B. FILE *fp = fopen("log.txt", "w"); char line[100]; while(fgets(line, 100, fp) != NULL) { printf("%s", line); } fclose(fp);
C. FILE *fp = fopen("log.txt", "r"); char line[100]; while(fgets(line, 100, fp) != NULL) { printf("%s", line); } fclose(fp);
D. FILE *fp = fopen("log.txt", "r"); char line[100]; while(fgets(line, 100, fp)) { printf(line); } fclose(fp);

Solution

  1. Step 1: Check file open mode

    Opening with "r" is correct for reading; "w" would overwrite the file.
  2. Step 2: Validate reading loop and printing

    Using fgets with NULL check is correct; printf with "%s" is safe and prints lines properly.
  3. Step 3: Review other options

    FILE *fp = fopen("log.txt", "w"); char line[100]; while(fgets(line, 100, fp) != NULL) { printf("%s", line); } fclose(fp); uses wrong mode "w"; FILE *fp = fopen("log.txt", "r"); char c; while((c = fgetc(fp)) != EOF) { printf("%c", c); } fclose(fp); reads char by char, not lines; FILE *fp = fopen("log.txt", "r"); char line[100]; while(fgets(line, 100, fp)) { printf(line); } fclose(fp); uses printf(line) without format string, unsafe.
  4. Final Answer:

    Correct reading lines with fgets and printing with printf("%s", line). -> Option C
  5. Quick Check:

    Use fgets + printf("%s") + fopen with "r" [OK]
Hint: Use fgets in loop with NULL check and printf("%s") [OK]
Common Mistakes:
  • Opening file with wrong mode "w" for reading
  • Using printf without format string
  • Reading char by char when lines needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard