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?
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?
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?
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()