0
0
Cprogramming~20 mins

Writing to files in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Writing 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 program writing to a file?

Consider the following C code that writes to a file named output.txt. What will be the content of the file after running this program?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("output.txt", "w");
    if (fp == NULL) {
        return 1;
    }
    fprintf(fp, "Hello\n");
    fprintf(fp, "World\n");
    fclose(fp);
    return 0;
}
A
The file contains:
Hello
World
B
The file contains:
HelloWorld
CThe program crashes and no file is created
DThe file is empty
Attempts:
2 left
💡 Hint

Look at how fprintf is used with newline characters.

Predict Output
intermediate
2:00remaining
What error does this code produce when writing to a file?

What error will this C code produce when executed?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("readonly.txt", "r");
    fprintf(fp, "Trying to write\n");
    fclose(fp);
    return 0;
}
ASegmentation fault at fprintf
BRuntime error: writing to a file opened in read mode
CNo error, file is written successfully
DCompilation error: fprintf requires a writable file pointer
Attempts:
2 left
💡 Hint

Think about the mode used to open the file and what fprintf expects.

🔧 Debug
advanced
2:00remaining
Why does this file writing code produce no output in the file?

The following C code is supposed to write "Data" to data.txt, but the file remains empty after running. What is the cause?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "w");
    if (fp == NULL) {
        return 1;
    }
    fputs("Data", fp);
    // Missing fclose(fp);
    return 0;
}
AThe file pointer is NULL, so nothing is written
BThe file is opened in write mode but no data is written
Cfputs does not write to files, only to stdout
DThe file is not closed, so output is buffered and not flushed to disk
Attempts:
2 left
💡 Hint

Think about how buffered output works and what happens if you don't close the file.

📝 Syntax
advanced
2:00remaining
Which option contains a syntax error in file writing code?

Identify the option that contains a syntax error preventing compilation.

AFILE *fp = fopen("file.txt", "w") if (fp == NULL) { return 1; } fprintf(fp, "Hello"); fclose(fp);
BFILE *fp = fopen("file.txt", "w"); if (fp == NULL) { return 1; } fprintf(fp, "Hello"); fclose(fp);
CFILE *fp = fopen("file.txt", "w"); if (fp != NULL) { fprintf(fp, "Hello"); fclose(fp); }
D;)pf(esolcf ;)"olleH" ,pf(ftnirpf } ;1 nruter { )LLUN == pf( fi ;)"w" ,"txt.elif"(nepof = pf* ELIF
Attempts:
2 left
💡 Hint

Look carefully for missing punctuation in the code.

🚀 Application
expert
2:00remaining
How many bytes are written to the file by this program?

This C program writes a string to a file. How many bytes will the file contain after running?

C
#include <stdio.h>

int main() {
    FILE *fp = fopen("test.txt", "w");
    if (!fp) return 1;
    const char *str = "Line1\nLine2\n";
    int count = fprintf(fp, "%s", str);
    fclose(fp);
    return 0;
}
A10 bytes
B11 bytes
C12 bytes
D13 bytes
Attempts:
2 left
💡 Hint

Count all characters including newline characters in the string.