Nearby lessons
70 of 72C - Writing to a File
Writing to a file allows a C program to save information for later use. Learn how to write formatted data with fprintf() and text with fputs().
1. Write Data Using fprintf()
fprintf() works like printf(), but sends the formatted output to a file instead of the screen.
Example01
2. Write a String Using fputs()
Use fputs() when you want to write a string. It does not add a new line automatically, so include \n if you need one.
Example02
📝 Key Takeaways
- Use fopen() with w mode to write a new file or replace existing contents.
- fprintf() writes formatted data to a file.
- fputs() writes a string to a file.
- Check fopen() for NULL and close the file after writing.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3