Bird
Raised Fist0
Cprogramming~10 mins

File modes in C - Interactive Code Practice

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("data.txt", "[1]");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aw
Brw
Ca
Dr
Attempts:
3 left
💡 Hint
Common Mistakes
Using "w" which opens the file for writing and truncates it.
Using "a" which opens the file for appending.
2fill in blank
medium

Complete the code to open a file for writing, creating it if it doesn't exist.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("output.txt", "[1]");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }
    fprintf(file, "Hello, World!\n");
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Ar
Bw
Ca
Dr+
Attempts:
3 left
💡 Hint
Common Mistakes
Using "r" which opens the file for reading only.
Using "a" which appends instead of overwriting.
3fill in blank
hard

Fix the error in the code to open a file for appending text.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("log.txt", "[1]");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }
    fprintf(file, "New log entry\n");
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aa
Bw
Cr
Dw+
Attempts:
3 left
💡 Hint
Common Mistakes
Using "w" which overwrites the file.
Using "r" which opens for reading only.
4fill in blank
hard

Fill both blanks to open a file for reading and writing without truncating it.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("data.txt", "[1][2]");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }
    // Read and write operations
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Ar
Bw
C+
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using "w+" which truncates the file.
Using "a+" which opens for appending.
5fill in blank
hard

Fill both blanks to open a file for writing and reading, truncating it if it exists.

C
#include <stdio.h>

int main() {
    FILE *file = fopen("report.txt", "[1][2]");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }
    // Write and read operations
    fclose(file);
    return 0;
}
Drag options to blanks, or click blank then click option'
Ar
B+
Cw
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using "r+" which does not truncate the file.
Using "a+" which appends instead of truncating.

Practice

(1/5)
1. Which file mode in C opens a file for reading only, without allowing any changes to the file?
easy
A. "w" mode
B. "r" mode
C. "a" mode
D. "rw" mode

Solution

  1. Step 1: Understand file modes in C

    "r" mode opens a file for reading only, without modifying it.
  2. Step 2: Compare other modes

    "w" mode opens for writing and clears the file; "a" mode appends; "rw" is invalid in C.
  3. Final Answer:

    "r" mode -> Option B
  4. Quick Check:

    Read-only mode = "r" [OK]
Hint: Read-only files use "r" mode in C [OK]
Common Mistakes:
  • Confusing "w" with read mode
  • Using "rw" which is invalid in C
  • Thinking "a" allows reading
2. Which of the following is the correct syntax to open a file named data.txt for writing in C?
easy
A. FILE *fp = fopen("data.txt", "w");
B. FILE *fp = fopen("data.txt", "r");
C. FILE *fp = fopen("data.txt", "a");
D. FILE *fp = open("data.txt", "w");

Solution

  1. Step 1: Identify correct function and mode

    In C, fopen opens files; "w" mode opens for writing.
  2. Step 2: Check other options

    FILE *fp = fopen("data.txt", "r"); uses "r" (read), C uses "a" (append), D uses wrong function open.
  3. Final Answer:

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

    Write mode uses fopen with "w" [OK]
Hint: Use fopen with "w" to write files in C [OK]
Common Mistakes:
  • Using open() instead of fopen()
  • Confusing "r" with write mode
  • Using "a" when write mode is needed
3. What will be the output of the following C code snippet?
FILE *fp = fopen("test.txt", "w");
fputs("Hello", fp);
fclose(fp);

fp = fopen("test.txt", "a");
fputs(" World", fp);
fclose(fp);

fp = fopen("test.txt", "r");
char buffer[20];
fgets(buffer, 20, fp);
printf("%s", buffer);
fclose(fp);
medium
A. Hello
B. Error
C. Hello World
D. World

Solution

  1. Step 1: Write "Hello" to file with "w" mode

    Opening with "w" clears file and writes "Hello".
  2. Step 2: Append " World" with "a" mode

    Opening with "a" adds " World" at the end without erasing.
  3. Step 3: Read file content with "r" mode

    Reading file returns "Hello World" in buffer.
  4. Final Answer:

    Hello World -> Option C
  5. Quick Check:

    Write + Append + Read = "Hello World" [OK]
Hint: "w" clears file, "a" adds at end, reading shows combined text [OK]
Common Mistakes:
  • Assuming "a" overwrites file
  • Forgetting "w" clears file
  • Expecting only "Hello" output
4. Identify the error in this C code snippet that tries to open a file for appending:
FILE *fp;
fp = fopen("log.txt", "wa");
if (fp == NULL) {
    printf("Failed to open file\n");
}
medium
A. Incorrect file mode string "wa"
B. Missing fclose(fp) after opening
C. Using printf instead of perror
D. File name should be absolute path

Solution

  1. Step 1: Check file mode string validity

    "wa" is not a valid mode; modes must be "r", "w", "a" or with "b" for binary.
  2. Step 2: Understand consequences

    Invalid mode causes fopen to fail and return NULL.
  3. Final Answer:

    Incorrect file mode string "wa" -> Option A
  4. Quick Check:

    File modes must be valid strings like "a" or "w" [OK]
Hint: File modes are single valid strings like "r", "w", or "a" [OK]
Common Mistakes:
  • Combining modes incorrectly like "wa"
  • Ignoring fopen failure check
  • Assuming "wa" means write and append
5. You want to create a log file that preserves old logs and adds new entries each time your program runs. Which file mode should you use in C to open the file, and why?
hard
A. "w" mode, because it clears old logs and writes fresh data
B. "r" mode, because it reads existing logs before adding new ones
C. "r+" mode, because it allows reading and writing at the start
D. "a" mode, because it appends new logs preserving old content

Solution

  1. Step 1: Understand log file requirements

    Logs must keep old entries and add new ones without erasing.
  2. Step 2: Match file mode to requirement

    "a" mode appends data at the end, preserving existing content.
  3. Step 3: Evaluate other modes

    "w" clears file, "r" only reads, "r+" reads/writes but doesn't append automatically.
  4. Final Answer:

    "a" mode, because it appends new logs preserving old content -> Option D
  5. Quick Check:

    Append mode "a" preserves and adds data [OK]
Hint: Use "a" mode to add without deleting old file content [OK]
Common Mistakes:
  • Using "w" which erases old logs
  • Thinking "r" can write
  • Confusing "r+" with append 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