0
0
Cprogramming~20 mins

Reading from files in C - Practice Problems & Coding Challenges

Choose your learning style9 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.