Want to save your data? in a File? That’s how we make and use files in C!
Welcome to the Newbie Programmer Series. Yes I know it’s too long I have posted anything. I apologize, but here I am with a new and interesting topic. In the last part, we have finished Arrays and Structures. From now, we will discuss File Management in C. Its about saving data the user has made. For example, when we play video games, how does the game remembers our highest score? That’s we are going to deal with.
So if you are new to this series, please check out all the previous parts (here) So that you can easily understand what insanity is going down below…!
Whenever computer does anything, it needs memory to save data. For example, when we make our code like “int x = 10;”, We are actually storing 10 in the computer’s memory. We already know this far.
Let’s dig a bit deeper. Computer memory is broadly classified as Primary and Secondary Memories.
Primary Memory is where our programs runs, the “RAM” of our computer. It is very fast. But it is temporary. Various types of programs first get loaded into RAM, then are shown to us.
That is why it is temporary. As soon the program is complete, the memory is deleted to make room for the other programs. When we make “int x = 10;”, this “10” is actually saved in RAM. But then, as soon as we close our program, this “10” is removed. And the free space is then used by other programs. That is why we say that higher RAM size brings Higher Performance in gaming and other applications.
But now suppose we want to save some information permanently into the computer. This is where Secondary Memory comes into play. Secondary Memory is in fact the Hard Disks (Those C: D: you see in windows), Pen Drives, CDs, DVDs etc. These are permanent data.And they are not deleted if the program is closed.
So suppose if we want to data like student’s score table, a normal program will first save the data into variables in RAM, and when you close the program, the data will be lost. But, we can also save the data in the hard disks permanently so that it can be accessed later. For that, we have to use Files.
The data we store in the secondary memory is actually stored as a single entity called as file, which has all the ones and zeros! Can you give some example? The images and movies you download, are files. Web Page is a file. Even the games we play is a file. Whatever we can get access to is file actually.
So to save any data, what we will do is that we will create a file. And then will write all the data we want into it. It will be saved as it is for the further use.
C language provides good and enough ways to make and use files.
The header “stdio.h” contains the standard functions for the file management.
As we have already discussed only theory in this whole post, let’s keep it theoretical :p !
C has to do a set of steps every time for file handling.
And that steps are really very easy. Let us discuss an example here, just theoretically. We will discuss it in detail in the next parts. Just check out this below code:
#include <stdio.h>
main()
{
FILE *fp;
fp = fopen("test.txt", "w");
fprintf(fp, "Hello, World!\n");
fclose(fp);
return 0;
}
Now let us try to understand those four steps.
Don’t push your brain hard, we will discuss them in detail in next part.
When you compile and run the program, you will see a new file will be created in the same folder, named “test.txt”. This can be opened in Note Pad, and you will find the message “Hello, World!”.
Now even if the program is closed, or the computer is turned off, this data will be kept forever until you delete it!
This is the basic idea of how we will be dealing with Files. We have seen just a theoretical example but we will understand the real applications in the next part.
So Stay Connected! :)
Previous Chapter: « Structure some Examples
© Shubham Ramdeo, 2015-2025.
All Rights Reserved.