Practice Top C Programming Questions on File Handling
Q1. Write a program to create a file and write "Hello, World!" into it.
Expected Output: A file named output.txt containing "Hello, World!".
Q2. Write a program to read and display the contents of a file.
Input: A file named input.txt containing "C programming is fun!".
Output: C programming is fun!
Q3. Write a program to append "Appending this line!" to an existing file.
Input: A file named log.txt.
Output: The file contains the appended line.
Q4. Write a program to copy the contents of one file into another.
Input: source.txt contains "Copy this text.".
Output: destination.txt contains "Copy this text.".
Q5. Write a program to count the number of characters in a file.
Input: A file named data.txt containing "Hello".
Output: Number of characters: 5
Q6. Write a program to count the number of words in a file.
Input: file.txt contains "C programming is fun".
Output: Number of words: 4
Q7. Write a program to count the number of lines in a file.
Input: A file with 3 lines.
Output: Number of lines: 3
Q8. Write a program to check if a file exists.
Input: Check for test.txt.
Output: File exists. or File does not exist.
Q9. Write a program to read a file and print its content in reverse order.
Input: sample.txt contains "ABC".
Output: CBA
Q10. Write a program to write numbers from 1 to 10 into a file.
Output: A file named numbers.txt containing "1 2 3 4 5 6 7 8 9 10".
Q11. Write a program to search for a specific word in a file.
Input: File contains "Learn C programming". Search for "C".
Output: Word found.
Q12. Write a program to convert the content of a file to uppercase.
Input: File contains "hello".
Output: File updated to "HELLO".
Q13. Write a program to convert the content of a file to lowercase.
Input: File contains "HELLO".
Output: File updated to "hello".
Q14. Write a program to write a structure containing student details to a file.
Input: Name: John, Age: 20.
Output: File contains student details.
Q15. Write a program to read a structure containing student details from a file.
Output: Displays the student details.
Q16. Write a program to delete a file.
Input: delete_me.txt
Output: File deleted successfully.
Q17. Write a program to rename a file.
Input: Rename oldname.txt to newname.txt.
Output: File renamed.
Q18. Write a program to count the occurrences of a specific character in a file.
Input: Count 'e' in "Hello there".
Output: Occurrences of 'e': 3
Q19. Write a program to write even numbers between 1 and 20 to a file.
Output: 2 4 6 8 10 12 14 16 18 20
Q20. Write a program to merge the contents of two files into a third file.
Input: file1.txt and file2.txt.
Output: merged.txt contains content from both.
Q21. Write a program to count the number of vowels in a file.
Input: File contains "Hello".
Output: Vowels: 2
Q22. Write a program to find the longest word in a file.
Input: File contains "Hello world!".
Output: Longest word: Hello
Q23. Write a program to display the size of a file in bytes.
Input: example.txt
Output: File size: 15 bytes
Q24. Write a program to check if a file is empty.
Input: empty.txt
Output: File is empty. or File is not empty.
Q25. Write a program to write some text into a file and then read and display it.
Output: Displays the text written.
Q26. Write a program to read a file and display its contents in sorted order.
Input: File contains "banana apple cherry".
Output: apple banana cherry
Q27. Write a program to replace a word in a file.
Input: Replace "old" with "new".
Output: File updated.
Q28. Write a program to count the number of non-alphanumeric characters in a file.
Input: "C@2024!"
Output: Non-alphanumeric characters: 2
Q29. Write a program to read and display the contents of a file line by line.
Output: Each line displayed separately.
Q30. Write a program to create multiple files based on user input.
Input: Number of files = 3.
Output: 3 files created.
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!