C Programs on File Handling

C Programming Examples - File Handling

Files are commonly used to store data that can be processed by programs. In order to save and restore data indefinitely, files must be used. There are two types of files in the C language: text files and binary files. Text files have a .txt extension. The contents of the file are plain text that can be easily modified or deleted. Binary files are more difficult and safer to read than text files because they store data in binary format (0 & 1) and can contain more data.

File Operations:

File Handling in C allows us to create, update, read, and delete files stored on the local file system. Some of the operations that can be performed on a file are as follows:

  • Create a new file
  • Open an existing file
  • Read from file
  • Write to file
  • Close the file

File Handling Functions:

The most commonly used file functions in C are listed below:

advertisement
advertisement
  • fopen() – It is used to open a new or existing file.
  • fscanf() – It is used to read information from a file.
  • fprintf() – It is used to write data from a file.
  • fgetc() – It is used to read a character from a file.
  • fputc() – It is used to write a character from a file.
  • fclose() – It is used to close a file.
  • fseek() – It is used to move the file pointer to a certain location.
  • ftell() – It is used to get the current position.
  • rewind() – It is used to put the file pointer at the beginning of a file

The following section contains a collection of C programs for file creation, deletion, and file operations such as count, convert, replace, and reverse. It also contains various C programs for appending, merging, creating, and updating files. Each sample program on the file handling includes a program description, C code, and program output. All examples have been compiled and tested on Windows and Linux systems.

Here is the listing of C programming examples on File Handling:

  1. C Programs on File Creation & Deletion
  2. C Programs on File Operations
  3. C Programs on Appending and Merging the Files
  4. C Programs on Count, Convert, Replace, and Reverse Operations in File
  5. C Programs on Creation and Updating Operations in File

1. C Programs on File Creation & Deletion

Program Description
Program to Create a File & Store Data in C C Program to Create a File and Store Information
Program to Read Data from File in C C Program to Read Data from File
Program to Delete Specific Line from File in C C Program to Delete a Specific Line from File

2. C Programs on File Operations

Program Description
Copy One File to Another File in C C Program to Copy One File to Another File
Count Number of Lines in Text File in C C Program to Count the Number of Lines in Text File
Capitalize First Letter of Each Word in File in C C Program to Capitalize First Letter of Each Word in a File
Find File Size using File Handling Function in C C Program to Find the File Size using File Handling Function
List Files in Directory in C C Program to List Files in Directory
Compare two Binary Files in C C Program to Compare Two Binary Files
Count Lines, Blank Lines, Lines ending with Semicolon in C C Program to Count Lines, Blank Lines, Lines Ending with Semicolon

3. C Programs on Appending and Merging the Files

Program Description
Append Contents of One File to Another in C C Program to Append the Content of One File to the End of Another File
Merge Alternate Lines from Two Files in C C Program to Merge Alternate Lines from Two Files
Sum of Numbers in Command Line Arguments in C C Program to Find Sum of Numbers in Command Line Arguments Recursively
Merge Two Files in C C Program to Merge Contents of Two Files into a Third File
Find the Function Name of a C File in C C program to Find the Function Name of a C File

advertisement

4. C Programs on Count, Convert, Replace, and Reverse Operations in File

Program Description
Replace Specific Line in File in C C Program to Replace a Specific Line in a Text File
Convert Text File to Lowercase in C C Program to Convert the Content of File to Lowercase
Convert Text File to Uppercase in C C Program to Convert the Content of File to UpperCase
Print Contents of File in Reverse Order in C C Program to Print the Contents of File in Reverse Order
Count Words using Command Line Argument in C C Program to Count the Total Number of Words using Command Line Argument

5. C Programs on Creation and Updating Operations in File

Program Description
Create and Update Employee Record in C C Program to Create Employee Record System using File Handling
Employee Record System in C C Program to Generate Employee Filename Records Extracted from Command Line Arguments
Employee Details Update using Files in C C Program to Update Details of Employee using Files
Grocery Store Inventory Program in C C Program to Display the Inventory of Items in a Store

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.