File Handling in C Interview Questions and Answers (Top 50 Questions for Freshers and Experienced)

File Handling in C Interview Questions and Answers – Top 50 Questions

File handling is one of the most important topics in C programming and is frequently asked during technical interviews for software development, embedded systems, and programming roles. Understanding how files are created, read, written, updated, and managed helps developers store data permanently instead of relying on temporary memory. This guide covers the Top 50 File Handling in C Interview Questions and Answers for beginners, freshers, and experienced professionals.

File handling in C allows programs to store and retrieve data permanently. Understanding file pointers, file opening modes, reading and writing functions, and random file access is essential for programming interviews. Mastering these concepts helps candidates perform well in software development, embedded systems, and C programming interviews.

File Handling in C Interview Questions and Answers

File handling is one of the most important topics in C programming and is frequently asked during technical interviews for software development, embedded systems, and programming roles. Understanding how files are created, read, written, updated, and managed helps developers store data permanently instead of relying on temporary memory. This guide covers the Top 50 File Handling in C Interview Questions and Answers for beginners, freshers, and experienced professionals.
1. What is file handling in C, and why is it important?
File handling is the process of storing and retrieving data from files using C programming.
    registor_now_P    
Importance:
  • Permanent data storage
  • Data sharing between programs
  • Efficient handling of large datasets
  • Data backup and recovery
2. What is a file pointer? Why is it required in file operations?
A file pointer is a pointer variable used to access and manipulate files.
FILE *fp;
It keeps track of the current position within the file.
3. How do you declare a file pointer in C?
FILE *fp;
The FILE structure is defined in stdio.h.
4. What happens if fopen() fails to open a file?
It returns NULL.
fp = fopen("data.txt","r");

if(fp == NULL)
{
    printf("File cannot be opened");
}
5. What is the difference between text files and binary files?
Text File Binary File
Stores data as characters Stores data in binary format
Human readable Not human readable
Larger size Smaller size
Slower access Faster access
6. Explain the different file opening modes in C.
Mode Description
r Read only
w Write only
a Append
r+ Read and write
w+ Read and write, overwrite
a+ Read and append
    Explore Courses - Learn More    
7. What is the difference between “r” and “r+” modes?
  • r → Read only
  • r+ → Read and write
Both require the file to exist.
8. What is the difference between “w” and “a” modes?
  • w overwrites existing content.
  • a adds data at the end of the file.
9. What is the purpose of fclose()?
It closes an opened file and releases resources.
fclose(fp);
10. What problems may occur if a file is not closed properly?
  • Data loss
  • Memory leaks
  • File corruption
  • Resource wastage
11. Differentiate between scanf() and fscanf().
  • scanf() reads from keyboard.
  • fscanf() reads from file.
12. What is the use of fgetc()?
Reads one character from a file.
char ch;
ch = fgetc(fp);
13. How is fgets() different from fgetc()?
  • fgetc() reads one character.
  • fgets() reads an entire line.
14. What is the purpose of fprintf()?
Writes formatted data to a file.
fprintf(fp,"%d",num);
15. Differentiate between fputc() and fputs().
  • fputc() writes one character.
  • fputs() writes a string.
16. Which function is used to write formatted data to a file?
fprintf()
17. What is the use of feof()?
Checks whether end of file is reached.
feof(fp);
18. What is random file access?
Accessing any location directly without reading sequentially.
19. What is the purpose of fseek()?
Moves the file pointer to a specific location.
fseek(fp,10,SEEK_SET);
20. Explain SEEK_SET, SEEK_CUR, and SEEK_END.
  • SEEK_SET → Beginning
  • SEEK_CUR → Current position
  • SEEK_END → End of file
21. What does ftell() return?
Returns the current position of the file pointer.
22. When would you use the rewind() function?
To move the file pointer back to the beginning.
23. How can you check whether a file exists in C?
FILE *fp = fopen("data.txt","r");

if(fp != NULL)
{
   printf("Exists");
}
24. What is the difference between sequential access and random access files?
  • Sequential access reads data in order.
  • Random access jumps directly to any location.
25. What are the advantages of file handling over storing data in variables?
  • Permanent storage
  • Large data capacity
  • Data sharing
  • Data backup

Additional Top 25 File Handling Interview Questions

26. Which header file is required for file handling?
#include <stdio.h>
27. What is a stream in C?
A stream is a flow of data between a program and a file.
28. Which function creates a new file?
fopen("file.txt","w");
29. Can fopen() create a file automatically?
Yes, in w, w+, a, and a+ modes.
30. What is the syntax of fopen()?
fopen("filename","mode");
31. What is the return type of fopen()?
FILE *
32. What is the use of fread()?
Reads binary data from a file.
33. What is the use of fwrite()?
Writes binary data to a file.
34. Why are binary files faster?
They store data in machine-readable format.
35. What is buffering in file handling?
Temporary storage used before data is written or read.
36. What is fflush()?
Forces buffered output to be written immediately.
37. What is EOF?
End Of File marker.
38. What value does EOF represent?
Usually -1.
39. How do you delete a file in C?
remove("file.txt");
40. How do you rename a file in C?
rename("old.txt","new.txt");
41. What is the use of putw()?
Writes an integer to a file.
42. What is the use of getw()?
Reads an integer from a file.
43. What is file corruption?
Damage to stored file data making it unusable.
44. What is file buffering?
Temporary memory used to improve file I/O performance.
45. What is the difference between fread() and fscanf()?
  • fread() reads binary data.
  • fscanf() reads formatted text data.
46. What is the difference between fwrite() and fprintf()?
  • fwrite() writes binary data.
  • fprintf() writes formatted text.
47. Can multiple files be opened simultaneously?
Yes, multiple file pointers can be used.
48. What is a file descriptor?
A unique identifier assigned by the operating system to an opened file.
49. Why should NULL be checked after fopen()?
To ensure the file opened successfully.
50. What are the common file handling functions in C?
  • fopen()
  • fclose()
  • fscanf()
  • fprintf()
  • fgetc()
  • fputc()
  • fgets()
  • fputs()
  • fread()
  • fwrite()
  • fseek()
  • ftell()
  • rewind()
  • feof()
 
Talk to Academic Advisor

Frequently Asked Questions

The most commonly used functions are fopen(), fclose(), fprintf(), fscanf(), fseek(), ftell(), fread(), and fwrite().

Text files store data in human-readable format, while binary files store data in machine-readable format for faster processing.

File handling enables permanent data storage, efficient data management, and sharing information between different program executions.

Author

Embedded Systems trainer – IIES

Updated On: 24-06-26


10+ years of hands-on experience delivering practical training in Embedded Systems and it's design