0
0
Cprogramming~20 mins

Opening and closing files - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Mastery Badge
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 when opening a file?

Consider the following C code snippet that attempts to open a file named data.txt in read mode. What will be printed if the file does not exist?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("File not found\n");
    } else {
        printf("File opened successfully\n");
        fclose(fp);
    }
    return 0;
}
AFile not found
BRuntime error
CCompilation error
DFile opened successfully
Attempts:
2 left
💡 Hint

Think about what happens when you try to open a file for reading that does not exist.

Predict Output
intermediate
2:00remaining
What happens if you try to close a file pointer that is NULL?

Look at this C code snippet. What will happen when it runs?

C
#include <stdio.h>

int main() {
    FILE *fp = NULL;
    fclose(fp);
    printf("Closed file pointer\n");
    return 0;
}
APrints "Closed file pointer" without error
BCauses a segmentation fault or runtime error
CCompilation error due to fclose argument
DPrints nothing and exits
Attempts:
2 left
💡 Hint

Consider what happens if you try to close a file pointer that was never opened.

🔧 Debug
advanced
3:00remaining
Why does this code fail to write to the file?

The following code tries to open a file for writing and write a string. However, the file remains empty after running. What is the problem?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("output.txt", "w");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }
    fputs("Hello, world!", fp);
    // Missing fclose here
    return 0;
}
Afputs requires a newline character to write
BThe file is opened in write mode but should be append mode
CThe string "Hello, world!" is not null-terminated
DThe file is not closed, so output is not flushed to disk
Attempts:
2 left
💡 Hint

Think about what happens to buffered output if the file is not closed.

📝 Syntax
advanced
2:00remaining
Which option correctly opens a file for appending text?

Choose the correct C code line that opens a file named log.txt for appending text data.

AFILE *fp = fopen("log.txt", "a");
BFILE *fp = fopen("log.txt", "x");
CFILE *fp = fopen("log.txt", "w+");
DFILE *fp = fopen("log.txt", "r+");
Attempts:
2 left
💡 Hint

Appending means adding data to the end without deleting existing content.

🚀 Application
expert
3:00remaining
How many times is the file opened and closed in this program?

Analyze the following C program. How many times does it open and close the file test.txt?

C
#include <stdio.h>

void write_once() {
    FILE *fp = fopen("test.txt", "w");
    if (fp) {
        fputs("First write\n", fp);
        fclose(fp);
    }
}

void write_twice() {
    FILE *fp = fopen("test.txt", "a");
    if (fp) {
        fputs("Second write\n", fp);
        fclose(fp);
    }
}

int main() {
    write_once();
    write_twice();
    return 0;
}
AThe file is opened twice but closed once
BThe file is opened once and closed once
CThe file is opened and closed twice
DThe file is opened once but closed twice
Attempts:
2 left
💡 Hint

Count each fopen and fclose call that executes.