0
0
Cprogramming~10 mins

Why file handling is required in C - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file for reading.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("example.txt", "[1]");
    if (file == NULL) {
        printf("Failed to open file.\n");
        return 1;
    }
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aw
Ba
Cr
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using "w" mode which opens file for writing and erases content.
Using "a" mode which opens file for appending.
2fill in blank
medium

Complete the code to write a string to a file.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "[1]");
    if (file == NULL) {
        printf("Cannot open file.\n");
        return 1;
    }
    fprintf(file, "Hello, file handling!\n");
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aw
Br
Ca
Drb
Attempts:
3 left
💡 Hint
Common Mistakes
Using "r" mode which is for reading only.
Using "rb" which is for reading binary files.
3fill in blank
hard

Fix the error in the code to append text to an existing file.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("log.txt", "[1]");
    if (file == NULL) {
        printf("Error opening file.\n");
        return 1;
    }
    fprintf(file, "New log entry\n");
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aw
Ba
Cw+
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Using "w" mode which overwrites the file.
Using "r" mode which does not allow writing.
4fill in blank
hard

Fill both blanks to read a character from a file and check for end of file.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("data.txt", "r");
    int ch;
    if (file == NULL) {
        printf("Cannot open file.\n");
        return 1;
    }
    while ((ch = [1](file)) != [2]) {
        putchar(ch);
    }
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Afgetc
BEOF
Cgetc
DNULL
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of EOF to check end of file.
Using getc without comparing to EOF.
5fill in blank
hard

Fill all three blanks to write and then read a string from a file.

C
#include <stdio.h>

int main() {
    char str[50];
    FILE *file = fopen("test.txt", "[1]");
    if (file == NULL) {
        printf("File open error.\n");
        return 1;
    }
    fprintf(file, "Hello World\n");
    fclose(file);

    file = fopen("test.txt", "[2]");
    if (file == NULL) {
        printf("File open error.\n");
        return 1;
    }
    fgets(str, sizeof(str), file);
    printf("Read from file: %s", [3]);
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aw
Br
Cstr
Dfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using "r" mode to write the file.
Printing the file pointer instead of the string.