0
0
Cprogramming~20 mins

File pointers in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Pointer 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 file pointer code?

Consider the following C code that writes and reads from a file using file pointers. What will be printed on the screen?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("test.txt", "w+");
    fputs("Hello, world!", fp);
    fseek(fp, 7, SEEK_SET);
    char buffer[6];
    fgets(buffer, 6, fp);
    printf("%s\n", buffer);
    fclose(fp);
    return 0;
}
Aworld
Bworld!
CHello
DHello,
Attempts:
2 left
💡 Hint

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

Predict Output
intermediate
2:00remaining
What does this code print after reading a file?

Given this code snippet, what will be the output?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");
    if (!fp) {
        printf("File not found\n");
        return 1;
    }
    fseek(fp, 0, SEEK_END);
    long size = ftell(fp);
    printf("%ld\n", size);
    fclose(fp);
    return 0;
}
A0
BThe size of the file in bytes
CFile not found
DUndefined behavior
Attempts:
2 left
💡 Hint

Think about what ftell returns after seeking to the end of the file.

🔧 Debug
advanced
2:00remaining
Why does this code cause a runtime error?

Examine the code below. It attempts to read a line from a file. Why does it cause a runtime error?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("input.txt", "r");
    char *line;
    if (fp == NULL) {
        return 1;
    }
    fgets(line, 100, fp);
    printf("%s", line);
    fclose(fp);
    return 0;
}
AThe pointer 'line' is uninitialized and points to an undefined memory location
BThe fgets call uses wrong parameters
CThe file pointer 'fp' is not checked for NULL
DThe file 'input.txt' does not exist
Attempts:
2 left
💡 Hint

Think about where line points before calling fgets.

📝 Syntax
advanced
2:00remaining
Which option causes a compilation error?

Which of the following code snippets will cause a compilation error related to file pointers?

AFILE *fp; fp = fopen("file.txt", "r"); fclose(fp);
BFILE *fp = fopen("file.txt", "r"); fclose(fp);
CFILE fp; fp = fopen("file.txt", "r"); fclose(&fp);
DFILE *fp = fopen("file.txt", "r"); if(fp) fclose(fp);
Attempts:
2 left
💡 Hint

Remember how FILE and FILE * types are used.

🚀 Application
expert
3:00remaining
How many characters are read by this code?

Given the file "example.txt" contains the text "ABCDE\nFGHIJ", how many characters will be printed by this program?

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

int main() {
    FILE *fp = fopen("example.txt", "r");
    char buffer[5];
    int count = 0;
    while (fgets(buffer, sizeof(buffer), fp)) {
        printf("%s", buffer);
        count += strlen(buffer);
    }
    fclose(fp);
    printf("\nTotal chars: %d\n", count);
    return 0;
}
A12
B10
C9
D11
Attempts:
2 left
💡 Hint

Remember that fgets reads up to n-1 characters and includes the newline if present.