File Handling in C – Complete
Question-wise Notes
Q1: What is file handling in C? Why is it important?
Answer:
File handling in C allows data to be stored permanently on storage devices like
hard disks. Without file handling, all data is lost once the program terminates. It
provides a way to create, open, read, write, and manipulate files.
Q2: What is fopen() function? How does it work?
Answer:
is used to open a file in a specific mode (read, write, append, etc.). It
fopen()
returns a file pointer.
Syntax:
FILE *fp = fopen("filename", "mode");
Modes:
"r" : Read mode
"w" : Write mode (creates new file or overwrites)
"a" : Append mode
"r+" , "w+" , "a+" : Read & write modes
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Unable to open file.\n");
return 1;
}
File Handling in C – Complete Question-wise Notes 1
fprintf(fp, "Hello File!");
fclose(fp);
return 0;
}
Output:
(data.txt file will contain "Hello File!")
Q3: What is fclose() ? Why is it necessary?
Answer:
fclose() is used to close an open file. It ensures that data is properly saved and
memory is freed.
Syntax:
fclose(fp);
Example:
FILE *fp = fopen("test.txt", "r");
// perform operations
fclose(fp);
Output: No output. File is properly closed.
Q4: What is fputc() in C? How does it work?
Answer:
fputc() writes a single character to a file.
Syntax:
fputc(character, fp);
Example:
File Handling in C – Complete Question-wise Notes 2
#include <stdio.h>
int main() {
FILE *fp = fopen("char.txt", "w");
fputc('A', fp);
fclose(fp);
return 0;
}
Output:
(char.txt will contain 'A')
Q5: What is fgetc() in C? How does it work?
Answer:
fgetc() reads a single character from a file.
Syntax:
char ch = fgetc(fp);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("char.txt", "r");
char ch = fgetc(fp);
printf("Read: %c\n", ch);
fclose(fp);
return 0;
}
Output:
Read: A
Q6: What is fprintf() and how is it different from printf() ?
File Handling in C – Complete Question-wise Notes 3
Answer:
fprintf() writes formatted output to a file, just like printf() does to the screen.
Syntax:
fprintf(fp, "format string", variables);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("info.txt", "w");
int roll = 101;
char name[] = "Alice";
fprintf(fp, "Name: %s\nRoll: %d", name, roll);
fclose(fp);
return 0;
}
Output:
(info.txt will contain:
Name: Alice
Roll: 101)
Q7: What is fscanf() in C? How does it work?
Answer:
fscanf() reads formatted input from a file, similar to scanf() .
Syntax:
fscanf(fp, "format string", &variables);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("info.txt", "r");
File Handling in C – Complete Question-wise Notes 4
char name[20];
int roll;
fscanf(fp, "Name: %s\nRoll: %d", name, &roll);
printf("Name: %s\nRoll: %d\n", name, roll);
fclose(fp);
return 0;
}
Output:
Name: Alice
Roll: 101
Q8: What is fgets() in C? How does it work?
Answer:
fgets() reads a string (line of text) from a file until a newline or EOF.
Syntax:
fgets(buffer, size, fp);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("msg.txt", "r");
char str[50];
fgets(str, 50, fp);
printf("Line: %s\n", str);
fclose(fp);
return 0;
}
Output:
Line: Hello File!
File Handling in C – Complete Question-wise Notes 5
Q9: What is fputs() in C? How does it work?
Answer:
fputs() writes a string to a file.
Syntax:
fputs("string", fp);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("note.txt", "w");
fputs("Welcome to C programming.", fp);
fclose(fp);
return 0;
}
Output:
(note.txt will contain "Welcome to C programming.")
Q10: What is fseek() in C? How does it work?
Answer:
fseek() moves the file pointer to a specific location in the file.
Syntax:
fseek(fp, offset, origin);
Parameters:
offset : Number of bytes to move
origin : Starting point ( SEEK_SET , SEEK_CUR , SEEK_END )
Example:
File Handling in C – Complete Question-wise Notes 6
#include <stdio.h>
int main() {
FILE *fp = fopen("seek.txt", "w+");
fputs("ABCDE", fp);
fseek(fp, 2, SEEK_SET);
fputc('Z', fp);
fclose(fp);
return 0;
}
Output:
(seek.txt will contain: ABZDE)
Q11: What is ftell() in C? How is it used?
Answer:
returns the current position of the file pointer (in bytes from the
ftell()
beginning).
Syntax:
long pos = ftell(fp);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("abc.txt", "w");
fputs("Hello", fp);
long pos = ftell(fp);
printf("Position: %ld\n", pos);
fclose(fp);
return 0;
}
Output:
File Handling in C – Complete Question-wise Notes 7
Position: 5
Q12: What is rewind() in C? What does it do?
Answer:
rewind() sets the file pointer back to the beginning of the file.
Syntax:
rewind(fp);
Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("test.txt", "r");
fgetc(fp); // read one character
rewind(fp); // go back to start
char ch = fgetc(fp);
printf("Character after rewind: %c\n", ch);
fclose(fp);
return 0;
}
Output:
Character after rewind: (first character of test.txt)
Q13: What is the use of fread() and fwrite() ?
Answer:
They are used to read/write blocks of binary data from/to files.
Syntax:
fread(ptr, size, count, fp);
fwrite(ptr, size, count, fp);
File Handling in C – Complete Question-wise Notes 8
Example (Binary Write/Read):
#include <stdio.h>
struct Student {
char name[20];
int roll;
};
int main() {
struct Student s1 = {"John", 1}, s2;
FILE *fp = fopen("bin.dat", "wb");
fwrite(&s1, sizeof(s1), 1, fp);
fclose(fp);
fp = fopen("bin.dat", "rb");
fread(&s2, sizeof(s2), 1, fp);
printf("Name: %s, Roll: %d\n", s2.name, s2.roll);
fclose(fp);
return 0;
}
Output:
Name: John, Roll: 1
Q14: What is the difference between text and binary mode in
file handling?
Answer:
Text Mode Binary Mode
Files are treated as a sequence of
Files are treated as a sequence of characters.
bytes.
Newline \n may be translated to platform- Data is stored and read as-is (no
specific (e.g., \r\n in Windows). translation).
Slower performance due to character Faster as there is no interpretation or
conversions. conversion.
Suitable for human-readable files like .txt , Suitable for non-text files like .jpg ,
.csv . .exe , .dat .
File Handling in C – Complete Question-wise Notes 9
Reading is done using functions like fgetc() , Reading is done using fread() and
fgets() , fscanf() . writing with fwrite() .
Modes include "rb" , "wb" , "ab" ,
Modes include "r" , "w" , "a" , "r+" , etc.
"rb+" , etc.
EOF is determined by a special character. EOF is determined by the byte count.
Cannot be edited using a normal text
Editing in a text editor is possible.
editor.
Q15: What are the common file opening modes?
Mode Description
"r" Open for reading
"w" Open for writing (creates new)
"a" Open for appending
"r+" Open for reading and writing
"w+" Create for read and write (overwrite)
"a+" Read and append
"rb"/"wb"/"ab" Binary modes
Q16: How to check if a file exists or failed to open?
Answer:
Use a null check after
fopen() .
Example:
FILE *fp = fopen("demo.txt", "r");
if (fp == NULL) {
printf("File does not exist!\n");
} else {
printf("File opened.\n");
fclose(fp);
}
Q17: Summary Chart of File Functions
File Handling in C – Complete Question-wise Notes 10
Function Description
fopen() Open a file
fclose() Close a file
fputc() Write a character
fgetc() Read a character
fprintf() Write formatted text
fscanf() Read formatted text
fputs() Write a string
fgets() Read a string
fseek() Move file pointer
ftell() Get current pointer position
rewind() Reset pointer to beginning
fread() Read binary data
fwrite() Write binary data
Q18: What is EOF in C? What is its significance?
Answer:
EOF stands for End Of File. It is a symbolic constant in C (usually defined as -1 )
that indicates the end of a file has been reached during input operations.
Significance of EOF:
1. To Detect End of File:
When reading from a file, functions like fgetc() , fscanf() , or fgets() return EOF
when there's no more data to read.
2. Used in Loops:
Commonly used in while loops to read files until the end is reached.
3. Improves Safety:
Prevents reading garbage values beyond the valid data.
Common Example:
#include <stdio.h>
int main() {
File Handling in C – Complete Question-wise Notes 11
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("File not found!\n");
return 1;
}
char ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
return 0;
}
Output:
(Displays all characters from data.txt until the file ends)
File Handling in C – Complete Question-wise Notes 12