C File Handling

C files I/O functions handle data on a secondary storage device, such as a hard disk.

C can handle files as Stream-oriented data (Text) files, and System oriented data (Binary) files.

Stream-oriented data filesThe data is stored in the same manner as it appears on the screen. The I/O operations like buffering, data conversions, etc. take place automatically.
System-oriented data filesSystem-oriented data files are more closely associated with the OS and data stored in memory without converting into text format.

C File Operations

Five major operations can be performed on file are:

  • Creation of a new file.
  • Opening an existing file.
  • Reading data from a file.
  • Writing data in a file.
  • Closing a file.

Steps for Processing a File

  • Declare a file pointer variable.
  • Open a file using fopen() function.
  • Process the file using the suitable function.
  • Close the file using fclose() function.

To handling files in C, file input/output functions available in the stdio library are:

FunctionUses/Purpose
fopenOpens a file.
fcloseCloses a file.
getcReads a character from a file
putcWrites a character to a file
getwRead integer
putwWrite an integer
fprintfPrints formatted output to a file
fscanfReads formatted input from a file
fgetsRead string of characters from a file
fputsWrite string of characters to file
feofDetects end-of-file marker in a file