Nearby lessons
65 of 72C - File Handling
File handling allows a C program to save information in a file and use it again later. In this lesson, you will learn why files are useful and the basic steps for working with files.
1. Why Do We Need Files?
Variables store data in memory while a program is running. When the program ends, that data is usually lost. Files help us save information so we can use it later.
Example: A student management program can save student names and marks in a file.
2. Basic Steps of File Handling
- Declare a file pointer.
- Open the file using
fopen(). - Check whether the file opened successfully.
- Read from or write to the file.
- Close the file using
fclose().
Example02
3. Common File Functions
fopen()opens a file.fprintf()writes formatted data.fputs()writes a string.fgets()reads a line of text.fclose()closes an opened file.
📝 Key Takeaways
- Files can keep information after a program ends.
- Include stdio.h to use basic file-handling functions.
- Use FILE * to work with an opened file.
- Check fopen for NULL and close opened files with fclose.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3