Bird
Raised Fist0
Cprogramming~10 mins

Why file handling is required in C - Test Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why is file handling required in C programming?
easy
A. To use more memory during execution
B. To make the program run faster
C. To save data permanently outside the program
D. To avoid using variables

Solution

  1. Step 1: Understand the purpose of file handling

    File handling allows programs to store data permanently on disk, beyond the program's runtime.
  2. Step 2: Compare options with this purpose

    Only saving data permanently matches the main reason for file handling; other options are unrelated.
  3. Final Answer:

    To save data permanently outside the program -> Option C
  4. Quick Check:

    File handling = permanent data storage [OK]
Hint: File handling = saving data permanently [OK]
Common Mistakes:
  • Thinking file handling speeds up program
  • Confusing memory use with file storage
  • Believing file handling avoids variables
2. Which of the following is the correct way to open a file for writing in C?
easy
A. FILE fp = fopen("data.txt", "w");
B. FILE *fp = fopen("data.txt", "r");
C. FILE *fp = open("data.txt", "w");
D. FILE *fp = fopen("data.txt", "w");

Solution

  1. Step 1: Recall fopen syntax in C

    fopen requires a FILE pointer and mode string; "w" mode opens file for writing.
  2. Step 2: Check each option

    FILE *fp = fopen("data.txt", "w"); uses FILE *fp and fopen with "w" correctly; others have wrong function, mode, or variable type.
  3. Final Answer:

    FILE *fp = fopen("data.txt", "w"); -> Option D
  4. Quick Check:

    Open file for writing = fopen with "w" [OK]
Hint: Use fopen with "w" mode and FILE pointer [OK]
Common Mistakes:
  • Using 'r' mode to write
  • Using open() instead of fopen()
  • Declaring FILE variable instead of pointer
3. What will be the output of this C code snippet?
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Hello World");
fclose(fp);
fp = fopen("test.txt", "r");
char str[20];
fgets(str, 20, fp);
printf("%s", str);
fclose(fp);
medium
A. Compilation error
B. Hello World
C. No output
D. Runtime error

Solution

  1. Step 1: Write and close file

    The code opens "test.txt" for writing, writes "Hello World", then closes the file.
  2. Step 2: Open file for reading and read content

    It reopens the file for reading, reads the string into str, and prints it.
  3. Final Answer:

    Hello World -> Option B
  4. Quick Check:

    Write then read file prints saved text [OK]
Hint: Write, close, reopen, read to get output [OK]
Common Mistakes:
  • Forgetting to close file before reading
  • Using wrong mode for fopen
  • Not reading file content before printing
4. Identify the error in this C code for file handling:
FILE *fp;
fp = fopen("data.txt", "r");
fprintf(fp, "Test");
fclose(fp);
medium
A. Using fprintf on a file opened in read mode
B. Not closing the file
C. Missing file pointer declaration
D. Using wrong filename

Solution

  1. Step 1: Check file open mode

    The file is opened in "r" mode, which is read-only.
  2. Step 2: Check usage of fprintf

    fprintf writes to file, but writing is not allowed in read mode, causing error.
  3. Final Answer:

    Using fprintf on a file opened in read mode -> Option A
  4. Quick Check:

    Write requires "w" mode, not "r" [OK]
Hint: Write only on files opened with "w" or "a" [OK]
Common Mistakes:
  • Writing to read-only file
  • Ignoring file open mode
  • Not checking fopen return value
5. You want to save user input permanently and add new entries without deleting old data. Which file mode should you use in C?
hard
A. "a" (append mode)
B. "r" (read mode)
C. "w" (write mode)
D. "r+" (read and write mode)

Solution

  1. Step 1: Understand file modes

    "w" overwrites file, "r" reads only, "a" appends data at end, "r+" reads and writes but does not append.
  2. Step 2: Choose mode for adding without deleting

    Append mode "a" adds new data without deleting existing content.
  3. Final Answer:

    "a" (append mode) -> Option A
  4. Quick Check:

    Append mode = add data without deleting [OK]
Hint: Use "a" mode to add data without erasing [OK]
Common Mistakes:
  • Using "w" which erases old data
  • Confusing "r+" with append mode
  • Trying to write in "r" mode
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard