The mode "r" opens a file for reading. The file must exist, or fopen() will return NULL.
Click to reveal answer
beginner
What happens if you open a file with mode "w" in C?
The mode "w" opens a file for writing. If the file exists, it is truncated (emptied). If it doesn't exist, a new file is created.
Click to reveal answer
intermediate
Explain the difference between "a" and "w" file modes.
"a" opens a file for appending. Data is added to the end without deleting existing content. "w" opens for writing and clears the file first.
Click to reveal answer
intermediate
What does the mode "r+" allow you to do?
"r+" opens a file for both reading and writing. The file must exist. You can read and write anywhere in the file.
Click to reveal answer
intermediate
What is the difference between "b" and text modes in file opening?
The "b" mode opens a file in binary mode, meaning data is read/written exactly as is. Without "b", the file is opened in text mode, which may translate line endings.
Click to reveal answer
Which file mode opens a file for reading and writing but requires the file to exist?
A"r+"
B"w"
C"a"
D"r"
✗ Incorrect
The "r+" mode opens a file for both reading and writing but the file must already exist.
What happens if you open a file with mode "w" and the file already exists?
AThe file is opened for reading only
BThe file is appended to
CThe file is truncated (emptied)
DThe file is left unchanged
✗ Incorrect
Opening with "w" clears the file content before writing.
Which mode should you use to add data to the end of a file without deleting existing content?
A"w"
B"a"
C"r"
D"r+"
✗ Incorrect
"a" mode appends data to the end of the file.
What does the "b" in file modes like "rb" or "wb" stand for?
ABinary mode
BBuffered mode
CBasic mode
DBlock mode
✗ Incorrect
"b" means binary mode, which reads/writes data exactly without translation.
If you try to open a non-existent file with mode "r", what happens?
AThe file is created
BThe file is opened for writing
CThe file is truncated
Dfopen() returns NULL
✗ Incorrect
Mode "r" requires the file to exist; otherwise fopen() returns NULL.
Describe the main differences between the file modes "r", "w", and "a" in C.
Think about what happens to the file content and if the file must exist.
You got /4 concepts.
Explain why and when you would use binary mode ("b") when opening files in C.
Consider files that are not plain text.
You got /3 concepts.
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
Step 1: Understand file modes in C
"r" mode opens a file for reading only, without modifying it.
Step 2: Compare other modes
"w" mode opens for writing and clears the file; "a" mode appends; "rw" is invalid in C.
Final Answer:
"r" mode -> Option B
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
Step 1: Identify correct function and mode
In C, fopen opens files; "w" mode opens for writing.
Step 2: Check other options
FILE *fp = fopen("data.txt", "r"); uses "r" (read), C uses "a" (append), D uses wrong function open.
Final Answer:
FILE *fp = fopen("data.txt", "w"); -> Option A
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?
Opening with "a" adds " World" at the end without erasing.
Step 3: Read file content with "r" mode
Reading file returns "Hello World" in buffer.
Final Answer:
Hello World -> Option C
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
Step 1: Check file mode string validity
"wa" is not a valid mode; modes must be "r", "w", "a" or with "b" for binary.
Step 2: Understand consequences
Invalid mode causes fopen to fail and return NULL.
Final Answer:
Incorrect file mode string "wa" -> Option A
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
Step 1: Understand log file requirements
Logs must keep old entries and add new ones without erasing.
Step 2: Match file mode to requirement
"a" mode appends data at the end, preserving existing content.
Step 3: Evaluate other modes
"w" clears file, "r" only reads, "r+" reads/writes but doesn't append automatically.
Final Answer:
"a" mode, because it appends new logs preserving old content -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.