What if your important data vanished just because you forgot to close a file?
Why Opening and closing files? - Purpose & Use Cases
Imagine you want to save your shopping list on your computer. Without opening and closing files properly, you might have to write everything down on paper every time or risk losing your list if the computer shuts down unexpectedly.
Manually handling data without opening and closing files means you can't save your work safely. It's like writing on a whiteboard and erasing it accidentally. You might lose important information, and it's slow to rewrite everything each time.
Opening a file lets your program access a place to read or write data. Closing the file tells the computer you're done, so it saves everything safely. This way, your data stays safe and your program runs smoothly.
printf("Enter data: "); scanf("%s", data); // No file saving
FILE *f = fopen("list.txt", "w"); fprintf(f, "%s", data); fclose(f);
It lets your programs save and retrieve information anytime, just like keeping notes in a safe notebook.
When you write a document in a text editor, it opens a file to save your work and closes it when you finish, so your changes are not lost.
Opening files connects your program to stored data.
Closing files ensures data is saved and resources are freed.
Without this, data loss and errors are common.