Challenge - 5 Problems
File Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this file error check?
Consider this C code snippet that tries to open a file and checks for errors. What will it print if the file does not exist?
C
#include <stdio.h> int main() { FILE *fp = fopen("nonexistent.txt", "r"); if (fp == NULL) { perror("Error opening file"); return 1; } fclose(fp); return 0; }
Attempts:
2 left
💡 Hint
Check what fopen returns when the file does not exist and how perror works.
✗ Incorrect
When fopen fails to open a file, it returns NULL. perror prints the string you give it, followed by a colon, a space, and the error message corresponding to errno. Since the file does not exist, it prints 'No such file or directory'.
❓ Predict Output
intermediate2:00remaining
What error does this code raise?
What error will this C code produce when trying to read from a file pointer that is NULL?
C
#include <stdio.h> int main() { FILE *fp = NULL; char buffer[10]; fgets(buffer, 10, fp); return 0; }
Attempts:
2 left
💡 Hint
Think about what happens if you try to use a NULL pointer with fgets.
✗ Incorrect
fgets expects a valid FILE pointer. Passing NULL causes undefined behavior, usually a segmentation fault because the program tries to access memory through a NULL pointer.
🧠 Conceptual
advanced2:00remaining
Which option correctly checks for file open errors?
You want to open a file for writing and handle errors properly. Which code snippet correctly checks if the file was opened successfully?
Attempts:
2 left
💡 Hint
Remember how to compare pointers to NULL in C.
✗ Incorrect
Option B correctly compares the FILE pointer to NULL using '=='. Option B incorrectly checks if fp is not NULL to detect error, which is wrong. Option B compares to 0, which is allowed but less clear; however, in C NULL is defined as 0, so it compiles but is less readable. Option B uses assignment '=' instead of comparison '==', causing a bug.
❓ Predict Output
advanced2:00remaining
What is the output of this code with error handling?
What will this program print if the file "data.txt" exists and contains the text "Hello"?
C
#include <stdio.h> int main() { FILE *fp = fopen("data.txt", "r"); if (!fp) { printf("Cannot open file\n"); return 1; } int c = fgetc(fp); if (c == EOF) { printf("Empty file\n"); } else { printf("First char: %c\n", c); } fclose(fp); return 0; }
Attempts:
2 left
💡 Hint
Check what fgetc returns when reading the first character of a non-empty file.
✗ Incorrect
Since the file exists and contains "Hello", fgetc reads the first character 'H'. It is not EOF, so the program prints 'First char: H'.
❓ Predict Output
expert2:00remaining
What is the value of 'count' after this file reading loop?
This program counts how many characters it reads from a file. What is the value of 'count' after running if the file "input.txt" contains exactly 3 characters?
C
#include <stdio.h> int main() { FILE *fp = fopen("input.txt", "r"); if (!fp) return 1; int count = 0; int ch; while ((ch = fgetc(fp)) != EOF) { count++; } fclose(fp); printf("%d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Remember that fgetc returns EOF after reading all characters.
✗ Incorrect
The loop increments count for each character read until EOF is reached. Since the file has 3 characters, count becomes 3.