- Creating, opening, reading, writing, and closing files using
fopen,fclose,fprintf,fscanf - Using text-mode vs binary-mode file operations
- Reading files character-by-character, line-by-line, and word-by-word
- Counting characters, words, and lines
- Searching for and replacing words in a text file
- Appending, merging, copying, and deleting lines in a file
- Reading and writing structured binary data with
freadandfwrite - Sorting file lines alphabetically
- Encrypting and decrypting file contents with simple substitution
- Random file access using
fseek,ftell, andrewind - Storing and retrieving records with structures inside binary files
- Building a small bank account management system with persistent storage
File handling is where C programs stop being toys and start doing real work. Whether you're writing logs, processing CSVs, storing records, or building a simple text editor, you need to be fluent with the FILE * family of functions. This page collects 21 file handling assignments, arranged from basic text I/O to binary processing, random access, structures in files, and a mini-project. Every assignment links to a full tutorial with the program code, sample output, and a line-by-line explanation.
🔹 Text File Basics
Start here — create, read, and count data in plain text files.
- 1. Write a program in C language to create a new text file and write some text in it.
- 4. Write a program in C to read and display the contents of a text file.
- 5. Write a program in C to count the number of characters, words, and lines in a text file.
- 14. Write a program in C to append text to an existing text file.
🔹 Text File Operations
Search, replace, merge, sort, copy, and delete content in text files.
- 2. Write a program in C to search for a specific word in a text file and display its occurrences.
- 6. Write a program in C to delete a specific line from a text file.
- 9. Write a program in C to merge multiple text files into a single text file.
- 10. Write a program in C to replace a word with another word in a text file.
- 12. Write a program in C language to sort the lines of a text file in alphabetical order.
- 15. Write a program in C to copy the contents of one text file into another text file.
🔹 Binary Files
Read and write raw structured data using binary-mode file operations.
- 3. Write a program in C to append data to an existing binary file.
- 7. Write a program in C to write data to a binary file.
- 8. Write a program in C to read binary data from a binary file and display it.
🔹 Encryption & Decryption
Apply simple character transformations to protect file contents.
- 11. Write a program in C to encrypt the contents of a text file.
- 13. Write a program in C to decrypt the contents of an encrypted text file.
🔹 Advanced & Project-Based
Interview favorites and mini-projects that combine file handling with structures, random access, and real-world use cases.
- 16. Write a program in C to store and retrieve student records using structures and binary files (
fwrite/fread). - 17. Write a program in C to demonstrate random file access using
fseek(),ftell(), andrewind(). - 18. Build a simple Bank Account Management System in C using file handling (create, deposit, withdraw, display).
- 19. Write a program in C to convert the contents of a text file to uppercase and save it to a new file.
- 20. Write a program in C to compress multiple consecutive spaces in a text file into a single space.
- 21. Write a program in C to print all odd-numbered lines of a file first, then all even-numbered lines.
How to Get the Most Out of These Assignments
Reading a solution is not the same as writing one. For each assignment, follow this three-step workflow:
- Attempt first. Open a blank editor and try to solve the problem using only the assignment description. Even if you get stuck, the act of trying cements the concept.
- Compare carefully. Open the solution page and compare your approach to the reference. Look for differences in file mode (
"r"vs"rb"), buffering, and error handling. - Re-implement from scratch. Close the solution page and write the program again without looking. This is the step most learners skip — and it's the one that actually builds fluency.
💡 Tip: Always check the return value of fopen against NULL before using the file pointer, and always fclose the file when you're done. Compile with gcc -Wall -Wextra -std=c11 -g so the compiler warns you about unused file handles and format-specifier mismatches.
❓ File Handling Viva Questions & Quick Answers
Q1. How do I check whether fopen failed?
Compare the returned pointer with NULL. If it is NULL, the file could not be opened — usually because it does not exist (in read mode) or you lack permission.
Q2. What is the difference between "w" and "a" mode?
"w" opens a file for writing and truncates it to zero length if it already exists. "a" opens a file for appending, keeping existing content and writing new data at the end.
Q3. Why must I call fclose()?
fclose() flushes any buffered data to disk and releases the file handle. Skipping it can cause data loss and exhaust available file descriptors.
Q4. What is the difference between text mode and binary mode?
In text mode, certain characters (like \n) may be translated by the OS. In binary mode ("rb", "wb"), bytes are read and written exactly as stored — essential for structs, images, and other raw data.
Q5. What does fseek(fp, 0, SEEK_END) do, and how do I find file size?
It moves the file position indicator to the end of the file. Calling ftell(fp) immediately afterward returns the file size in bytes.
Q6. Can I store a structure directly to a file?
Yes — with fwrite(&s, sizeof(struct myStruct), 1, fp) in binary mode. Read it back with fread using the same type and size. Avoid storing pointers inside such structures.