File handling programming exercises and solutions in C

In programming we use variables to store data. Variables are powerful at storing data, but they are volatile. They lose their value after program termination.

However, their exists situations where we want persistent data. The data should persist even after program termination and can be used anytime later. A file is a physical storage location on disk where data is persisted (stored permanently). We use file to store data permanently on hard disk, that can be used in future.

Large projects interacts with lot of files and directories to persist data permanently. As a beginner or as an exert we must know how to work with files. In this exercise I will explain how easily you can work with files and directories in C programming.

In programming we generally perform three operations on file:

  1. Open a file
  2. Process file (read, write, modify)
  3. Save and close file

So lets, get started and create our first file. Like I always say please feel free to ask your doubts or give your valuable suggestions in the comments section.

Required knowledge

Basic C programming, Structures, Pointers, File Handling

File handling in C programming

List of File handling programming exercises

  1. Write a C program to create a file and write contents, save and close the file.
  2. Write a C program to read file contents and display on console.
  3. Write a C program to read numbers from a file and write even, odd and prime numbers to separate file.
  4. Write a C program to append content to a file.
  5. Write a C program to compare two files.
  6. Write a C program to copy contents from one file to another file.
  7. Write a C program to merge two file to third file.
  8. Write a C program to count characters, words and lines in a text file.
  9. Write a C program to remove a word from text file.
  10. Write a C program to remove specific line from a text file.
  11. Write a C program to remove empty lines from a text file.
  12. Write a C program to find occurrence of a word in a text file.
  13. Write a C program to count occurrences of a word in a text file.
  14. Write a C program to count occurrences of all words in a text file.
  15. Write a C program to find and replace a word in a text file.
  16. Write a C program to replace specific line in a text file.
  17. Write a C program to print source code of same program.
  18. Write a C program to convert uppercase to lowercase character and vice versa in a text file.

  19. Write a C program to find properties of a file using stat() function.
  20. Write a C program to check if a file or directory exists.
  21. Write a C program to rename a file using rename() function.
  22. Write a C program to list all files and sub-directories recursively.

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>