Pages

Wednesday, December 1, 2010

File Handling

What is file handling ?
File Handling allows you to move the data from RAM to the hard disc. To define, file handling is the process of storing data in a storage device to use it later.

Why is it needed ?

The computer system has 2 types of memory - Primary and Secondary. Primary memory is further divided into RAM and ROM, whereas secondary memory includes all kind of storage devices like the hard disc, CD/DVD, flash drive etc. Of all these different type of "memories", the word memory is mainly use for RAM or the Random Access Memory. Anything that you see on the monitor is loaded from a storage device to the main memory or RAM. The data that we want to show on the screen must be loaded from the secondary memory to the primary memory.  For example, the moment you switch on your computer, it starts loading the operating system (Windows, Linux) to the main memory. The OS resides permanently on the Hard disc but it is of no use unless it gets loded into the RAM. Once it does, the computer starts working. This is called "booting" in computing terminology. All the variables that we declare in a program are also stored in RAM. Remember, that RAM is a volatile memory, which means that it can not store data permanently. Therefore if you ask the user to enter some details like, name, marks, etc., you won't be able to see the entered data once the computer is switched off and turned back on.
Let's see the following code to understand this better :

#include
void main()
{
    char player_name[12];
    int goals;

    printf("\nName of the player ");
    scanf("%s",&player_name);
    printf("\nRGoals Scored ");
    scanf("%d",&goals);
   
      printf("\nName of the player %s",player_name);   
    printf("Goals Scored %d",goals);
}

When we run the above code for the first time, suppose this is the data that we entered
    Name of the player Peter
    Goals Scored 3


The output will be
    Name of the player Peter
    Goals Scored 3


If you run the above program again, it will prompt you to enter the data again. Suppose this time you enter the following values

    Name of the player Joe
    Goals Scored 2

    Name of the player Joe
    Goals Scored 2


Now, note that when the program runs for the second time it doesn't print the data of the first player. Why ? Because the data you entered in the second run of the program (Joe and 2) it overwrote the data entered in the first time(Peter, 3). So when you ask it to print the names and goals, its prints the most recent values.  At the core of this problem is the difference between the primary and secondary memory. The data you are entering in the variables is being stored in the primary memory (RAM) and not in the secondary memory(hard disc, DVD etc.) RAM being temporary, overwrites the previous data once you enter the new data or simply close the program or switch off the system. If you want to the data to be saved permanently so that you can view/edit/ delete it in the future, you must save it to the secondary memory. To summarize, if you want to store the data permanently so that you can use it later, save the data in the secondary memory instead of the primary memory.

How?
A file is a "collection of data". This "data" can be in the form of text, graphics, audio or even a video clip.  There are 2 only operations that can be performed on a file - Reading and Writing.

Reading - is the process of either reading the data of the file fully or partially. Partial reading means reading only the desired data or part of the file.
Writing - is the process of either creating a new file or editing its contents. Editing includes modifying existing data, deleting data etc. It can be said that if you are not reading a file, you are writing it.

0 comments:

Post a Comment