w3resource

C Programming Exercises, Practice, Solution : File Handling


This resource offers a total of 95 C File Handling problems for practice. It includes 19 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Create & Store File

Write a program in C to create and store information in a text file.

Test Data :

Input a sentence for the file : This is the content of the file test.txt.

Expected Output :

 The file test.txt created successfully...!! 

Click me to see the solution


2. Read Existing File

Write a program in C to read an existing file.

Test Data :

Input the file name to be opened : test.txt

Expected Output :

 The content of the file test.txt is  :                                                                       
This is the content of the file test.txt.

Click me to see the solution


3. Write Multiple Lines to File

Write a program in C to write multiple lines to a text file.

Test Data :

Input the number of lines to be written : 4
:: The lines are ::
test line 1
test line 2
test line 3
test line 4

Expected Output :

 The content of the file test.txt is  :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4 

Click me to see the solution


4. Read File to Array

Write a program in C to read the file and store the lines in an array.

Test Data :

Input the file name to be opened : test.txt

Expected Output :

 The content of the file test.txt  are :                                                                      
 test line 1                                                                                                  
 test line 2                                                                                                  
 test line 3                                                                                                  
 test line 4

Click me to see the solution


5. Count Lines in File

Write a program in C to find the number of lines in a text file.

Test Data :

Input the file name to be opened : test.txt

Expected Output :

 The lines in the file test.txt are : 4 

Click me to see the solution


6. File Content & Line Count

Write a program in C to find the content of a file and the number of lines in a text file.

Test Data :

Input the filen ame to be opened : test.txt

Expected Output :

 The content of the file test.txt  are :                                                                      
 test line 1                                                                                                  
 test line 2                                                                                                  
 test line 3                                                                                                  
 test line 4                                                                                                  
 The lines in the file are : 4

Click me to see the solution


7. Count Words & Characters in File

Write a program in C to count the number of words and characters in a file.

Test Data :

Input the file name to be opened : test.txt

Expected Output :

Count the number of words and characters in a file:
---------------------------------------------------------
Input the filename to be opened: test1.txt
The content of the file test1.txt are:
test line 1
test line 2
test  line 3
test   line 4
The number of words in the file test1.txt are: 12
The number of characters in the file test1.txt are: 50

Click me to see the solution


8. Delete Specific Line from File

Write a program in C to delete a specific line from a file.

Assume that the content of the file test.txt is :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4                                                                                                   

Test Data :

Input the file name to be opened : test.txt
Input the line you want to remove : 2

Expected Output :

 The content of the file test.txt is :                                                                       
test line 1                                                                                                   
test line 3                                                                                                   
test line 4

Click me to see the solution


9. Replace Specific Line in File

Write a program in C to replace a specific line with another text in a file.

Assume that the content of the file test.txt is :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4                                                                                                   

Test Data :

Input the file name to be opened : test.txt
Input the content of the new line : Yes, I am the new text instead of test line 2
Input the line no you want to replace : 2

Expected Output :

Replacement did successfully..!! 

Click me to see the solution


10. Append Multiple Lines to File

Write a program in C to append multiple lines to the end of a text file.

Assume that the content of the file test.txt is :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4                                                                                                   

Test Data :

Input the file name to be opened : test.txt
Input the number of lines to be written : 3
The lines are :
test line 5
test line 6
test line 7

Expected Output :

The content of the file test.txt is  :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4                                                                                                   
                                                                                                              
test line 5                                                                                                   
test line 6                                                                                                   
test line 7

Click me to see the solution


11. Copy File to Another Name

Write a program in C to copy a file to another name.

Assume that the content of the file test.txt is :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4                                                                                                   

Test Data :

Input the source file name : test.txt
Input the new file name : test1.txt

Expected Output :

 The file test.txt  copied successfully in the file test1.txt.

If you read the new file you will see the content of the file :

test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4 

Click me to see the solution


12. Merge Two Files

Write a program in C to merge two files and write them to another file.

Assume that the content of the file test.txt and test1.txr is :                                                                       
 The content of the file test.txt is  : 
This is the file test.txt. 

 The content of the file test1.txt is  : 
This is the file test1.txt.                                                                                                    

Test Data :

Input the 1st file name : test.txt
Input the 2nd file name : test1.txt
Input the new file name where to merge the above two files : mergefiles.txt

Expected Output :

 The two files merged into mergefiles.txt file successfully..!! 

Here is the content of the merge file mergefiles.txt :

 The content of the file mergefiles.txt is  :                                                                 
This is the file test.txt.                                                                                    
This is the file test1.txt.

Click me to see the solution


13. Encrypt File

Write a program in C to encrypt a text file.

 Assume that, the content of the file test.txt is  :                                                                       
Welcome to w3resource.com. 

Test Data :

Input the name of file to encrypt : test.txt

Expected Output :

File test.txt successfully encrypted ..!!

If you read the file test.txt you will see the following :

������Ʉ�ӄۗ�������ɒ��ђn

Click me to see the solution


14. Decrypt File

Write a program in C to decrypt a previously encrypted file.

 Assume that, the content of the file test.txt was  :                                                                       
������Ʉ�ӄۗ�������ɒ��ђn 

After encryption, the content of the file is : 
Welcome to w3resource.com.

Test Data :

Input the name of file to decrypt : test.txt

Expected Output :

The file test.txt decrypted successfully..!!

Now, if you read the file test.txt you will see the following :

Welcome to w3resource.com.

Click me to see the solution


15. Delete File from Disk

Write a program in C to remove a file from the disk.

Test Data :

Input the name of file to delete : test.txt

Expected Output :

 The file test.txt is deleted successfully..!! 

Click me to see the solution


16. Common Directory Tree

A set of strings represent directory paths and a single character directory separator (/). Write a program in C language to get a part of the directory tree that is common to all directories.

Test Data:

({/home/me/user/temp/a", "/home/me/user/temp/b","/home/me/user/temp/c/d"}) ->
/home/me/user/temp

Click me to see the solution


17. File Read/Write Variable

Write a program in C to read from a file into a variable and write a variable's contents into a file.

Click me to see the solution


18. Display Last Modification Time

Write a program in C to display the last modification time of a file.

Test Data:

Last date of File modification: Sat Nov 26 17:32:15 2022

Click me to see the solution


19. Find File Size

Write a program in C language to find the size of a given file.

Test Data:

Size of the said File: 34 bytes.

Click me to see the solution


C Programming Code Editor:



More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.