File handling

In programming we use variables to store data. Variables are fast and quick at data storage and manipulation, but are volatile. They lose their value after program termination.


In our programming life, there 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. A directory is a logical logical path used to organize files. A file exists within a directory.
We perform only three operations on file:
  1. Open a file
  2. Process file (read, write, modify)
  3. Save and close file
So let us begin our file handling journey, with the following list of file handling exercises and solutions. Try to solve as much as you can.


List of file handling 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.

No comments

Powered by Blogger.