File Handling Assignments in C — 21 Programs with Solutions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

File Handling Assignments in C Language with Explanation and Solutions

📑 Jump to a category:
  • Text File Basics (1, 4, 5, 14)
  • Text File Operations (2, 6, 9, 10, 12, 15)
  • Binary Files (3, 7, 8)
  • Encryption / Decryption (11, 13)
  • Advanced & Project-Based (16–21)
  • Previous / Next Topic
📚 What you'll practice across these 21 assignments:
  • 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 fread and fwrite
  • Sorting file lines alphabetically
  • Encrypting and decrypting file contents with simple substitution
  • Random file access using fseek, ftell, and rewind
  • 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.

File handling assignments in C programming language with step-by-step explanation, solution, and description.

🔹 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(), and rewind().
  • 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:

  1. 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.
  2. 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.
  3. 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.

📖 Related Tutorials

  • File Handling In C
  • Types Of Files In C
  • Operations Done In File Handling
  • File Examples
  • Binary Files
  • Count Vowels In A File
  • Files FAQ
  • Files Multiple-Choice Questions

Previous Topic: -->> Pointers Assignments in C   ||   Next topic: -->> Storage Class Assignments in C


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.