Reading from files lets your program get information saved on your computer. This helps your program use data that was saved before or from other sources.
Reading from files in C
FILE *file_pointer; file_pointer = fopen("filename.txt", "r"); if (file_pointer == NULL) { // handle error } // read from file fclose(file_pointer);
fopen opens the file. The "r" means open for reading.
Always check if fopen returns NULL. That means the file could not be opened.
FILE *fp = fopen("data.txt", "r"); if (fp == NULL) { printf("Cannot open file\n"); }
char line[100]; while (fgets(line, sizeof(line), fp)) { printf("%s", line); }
int c; while ((c = fgetc(fp)) != EOF) { putchar(c); }
This program opens a file named example.txt for reading. It reads the file line by line and prints each line to the screen. If the file cannot be opened, it prints an error message.
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { printf("Failed to open file.\n"); return 1; } char line[256]; while (fgets(line, sizeof(line), file)) { printf("%s", line); } fclose(file); return 0; }
Remember to close the file with fclose() to free resources.
fgets() reads one line at a time including the newline character.
Use fgetc() to read one character at a time if needed.
Use fopen() with "r" to open a file for reading.
Check if the file opened successfully by testing if the pointer is NULL.
Read file content using fgets() for lines or fgetc() for characters.
Practice
fopen() function with mode "r" do in C?Solution
Step 1: Understand fopen modes
The mode "r" in fopen means open the file for reading only.Step 2: Check other modes
Modes like "w" are for writing, "a" for appending, and none delete files.Final Answer:
It opens a file for reading only. -> Option BQuick Check:
fopen with "r" = open for reading [OK]
- Confusing "r" with write mode "w"
- Thinking fopen deletes or creates files with "r"
- Not checking if file exists before reading
data.txt for reading in C?Solution
Step 1: Check fopen syntax
fopen requires two arguments: filename and mode as strings.Step 2: Identify correct mode for reading
"r" is the mode to open a file for reading.Final Answer:
FILE *fp = fopen("data.txt", "r"); -> Option AQuick Check:
Correct fopen syntax with "r" mode = FILE *fp = fopen("data.txt", "r"); [OK]
- Using "w" mode which opens for writing, not reading
- Omitting the mode argument in fopen
- Using "a" mode which opens for appending
input.txt contains the line "Hello"?
FILE *fp = fopen("input.txt", "r");
char buffer[10];
fgets(buffer, 10, fp);
printf("%s", buffer);
fclose(fp);Solution
Step 1: Understand fgets behavior
fgets reads a line including the newline character if it fits in the buffer.Step 2: Check file content and output
The file has "Hello" followed by a newline, so fgets reads "Hello\n" and prints it.Final Answer:
Hello\n -> Option AQuick Check:
fgets includes newline if present = Hello\n [OK]
- Assuming fgets removes newline automatically
- Confusing null terminator with newline
- Not checking if file opened successfully
FILE *fp = fopen("file.txt", "r");
char c;
c = fgetc(fp);
printf("%c", c);
fclose(fp);Solution
Step 1: Check file open validation
Always check if fopen returns NULL before reading to avoid errors.Step 2: Review other parts
fgetc is correct for reading one char; c as char is acceptable; fclose after reading is correct.Final Answer:
Missing check if fopen returned NULL before reading. -> Option DQuick Check:
Always check fopen result before file operations [OK]
- Skipping NULL check after fopen
- Confusing fgetc and fgets usage
- Closing file too early
log.txt and print them. Which code snippet correctly does this in C?Solution
Step 1: Check file open mode
Opening with "r" is correct for reading; "w" would overwrite the file.Step 2: Validate reading loop and printing
Using fgets with NULL check is correct; printf with "%s" is safe and prints lines properly.Step 3: Review other options
FILE *fp = fopen("log.txt", "w"); char line[100]; while(fgets(line, 100, fp) != NULL) { printf("%s", line); } fclose(fp); uses wrong mode "w"; FILE *fp = fopen("log.txt", "r"); char c; while((c = fgetc(fp)) != EOF) { printf("%c", c); } fclose(fp); reads char by char, not lines; FILE *fp = fopen("log.txt", "r"); char line[100]; while(fgets(line, 100, fp)) { printf(line); } fclose(fp); uses printf(line) without format string, unsafe.Final Answer:
Correct reading lines with fgets and printing with printf("%s", line). -> Option CQuick Check:
Use fgets + printf("%s") + fopen with "r" [OK]
- Opening file with wrong mode "w" for reading
- Using printf without format string
- Reading char by char when lines needed
Ready to prove it?
Pass the test and this pattern moves unknown
