0
0
Cprogramming~20 mins

File modes in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Modes 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 when opening a file in "w" mode?
Consider the following C code snippet that opens a file in write mode and writes a string. What will be the content of the file after running this code?
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("testfile.txt", "w");
    if (fp == NULL) return 1;
    fprintf(fp, "Hello, world!\n");
    fclose(fp);
    return 0;
}
AThe file "testfile.txt" will contain the text "Hello, world!\n".
BThe file "testfile.txt" will contain garbage data because the file was not opened in binary mode.
CThe file "testfile.txt" will be empty because "w" mode does not write data.
DThe program will crash because "w" mode cannot create files.
Attempts:
2 left
💡 Hint
Remember that "w" mode creates the file if it does not exist and truncates it if it does.
Predict Output
intermediate
2:00remaining
What happens when opening a file in "r" mode if the file does not exist?
What will the following C code print if "nofile.txt" does not exist?
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("nofile.txt", "r");
    if (fp == NULL) {
        printf("File not found\n");
    } else {
        printf("File opened\n");
        fclose(fp);
    }
    return 0;
}
AThe program crashes with segmentation fault
BFile opened
CFile not found
DThe program prints nothing
Attempts:
2 left
💡 Hint
Opening a file in "r" mode requires the file to exist.
Predict Output
advanced
2:00remaining
What is the output of this code using "a" mode to append to a file?
Assuming "append.txt" initially contains "Line1\n", what will be the content of the file after running this code?
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("append.txt", "a");
    if (fp == NULL) return 1;
    fprintf(fp, "Line2\n");
    fclose(fp);
    return 0;
}
AThe file is empty
B"Line1\nLine2\n"
C"Line2\n"
D"Line1\n"
Attempts:
2 left
💡 Hint
The "a" mode appends data to the end of the file without deleting existing content.
Predict Output
advanced
2:00remaining
What error does this code raise when opening a file with mode "r+" if the file does not exist?
Consider this code snippet. What will happen if "data.txt" does not exist?
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "r+");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    fclose(fp);
    return 0;
}
AError opening file: No such file or directory
BThe file is created and opened successfully
CSegmentation fault
DNo output, program runs silently
Attempts:
2 left
💡 Hint
The "r+" mode requires the file to exist for reading and writing.
Predict Output
expert
2:00remaining
What is the content of the file after this code runs with mode "w+"?
Given the file "example.txt" initially contains "OldContent\n", what will be the content after running this code?
C
#include <stdio.h>

int main() {
    FILE *fp = fopen("example.txt", "w+");
    if (fp == NULL) return 1;
    fprintf(fp, "NewContent\n");
    fseek(fp, 0, SEEK_SET);
    char buffer[20];
    fgets(buffer, sizeof(buffer), fp);
    printf("Read from file: %s", buffer);
    fclose(fp);
    return 0;
}
AThe program crashes
BRead from file: OldContent\n
CRead from file: NewContent\n
DRead from file: (empty line)
Attempts:
2 left
💡 Hint
Opening with "w+" truncates the file, but reading immediately after writing requires a flush or reposition.