In the previous session we discussed about memory, files, file handling, its uses etc. Now it's time to move ahead and learn how it is practically done in C.
In C to handle (read/write) a file, you must open it first. Since file is a collection of data, to open it, the variable must be efficient enough to handle all type of data. You can't declare an int variable to handle the contents of file since it may contain chars also. Similarly if the variable is of char type it won't be able to perform calculations. Therefore to open a file, we declare a variable using the "FILE" notation
exampleFILE *fptr;
This statement will declare a variable named "fptr" of FILE type. This variable should always be of pointer type since it will be pointing to a file. You can change the variable name "fptr" to any other valid name but the "FILE *" can not be changed. After declaration, the variable must be initialized just like all other variable are. The difference is you can not assign it a value using the "=" operator. For example, an integer variable can be assigned a value with a simple statement like "a=10" but not in case of FILE variables. To assign a value to a FILE variable, the function "fopen()" is used. The "fopen()" takes 2 arguments, the name of the file and the mode. This is how it looks
Syntax
fptr=fopen("filename","mode");
Example
fptr=fopen("hello","r"); // opening a file in read mode
fptr=fopen("file1.txt","w"); // opening a file in write mode.
make sure that you choose the right mode while opening the file. Mode denotes that for what purpose are you opening the file for , it can be reading (r), writing (w), appending (a) or any other pre-defined mode. Note that you can not perform any task other than for what file has been opened. It means that if the file has been opened for writing you can not read it and vice versa.
Reading a file
To read a file, we have multiple methods. We can read a file char by char, string by string or byte by byte. Which method you will choose depends on the type of data the file contains, for example if the file contains text data we can read it char or string wise but if the data is in binary form we will have to read it byte wise. Let's see all these methods one by one.
To read a file char by char :
If we want to read a file char by char the function used will be "fgetc()". The "fgetc()" function will read the current char from the file and advances to the next char. Remember that this function reads only a char at a time, so if you want to read the whole file you must place the "fgetc()" inside a loop and process it till the end of the file. The advantage of using the "fgetc()" is that since we are reading char by char we can make programs like counting how many alphabets are in the file,counting capital letters , small letters etc. Since it reads only one char at a time, it is not suitable for reading large files because it will take a lot of time to process such a file. Let's create a program to understand the working of "fgetc()"
#include
#include
void main()
{
FILE *fp; // Line 1
fp=fopen("file1","r"); // Line 2
char c; // Line 3
c=fgetc(fp); // Line 4
printf("%c",c); // Line 5
fclose(fp); // Line 6
}
Description
Line 1
Declares a pointer of FILE type. "fp" is just a variable name and can be changed.
Line 2
The second statement initialises the variable "fp" using the "fopen()". The first argument(file1) is the name of the file that we want to open for reading. Make sure that since we are opening this file for reading, this should be the name of an existing file. If this file is not available the whole program will go for a toss and nothing will be printed on screen. if the file to be read is located in the same directory(folder) as the program, the second statement is fine but if the source file is located else where we will have to specify the path like this - fp=fopen("c:\\my_progs\\file1"). Note the use of double back slashes (\\). Normally we use a single slash ("c:\my_progs\file1") , but C assumes anything written after a single slash as escape sequence like \n or \t so to inform the compiler that is not an escape sequence we put double slashes.
Line 4
Now comes the reading part. The fgetc() function will read the first byte (char) of the file and puts it in the variable "c". Suppose that the file contains all the alphabets from "a-z", fgetc() will read "a" and stores it to the variable "c"
Line 5
The value of "c" is now being printed. If the data inside it is the alphabet "a" the screen will read "a".
Now the question is why only "a" is being printed, why not the whole file. What should I do to print the whole file? The answer is - put fgetc() inside a loop. See the code below which prints the whole file char by char.
#include
#include
void main()
{
FILE *fp; // Line 1
fp=fopen("file1","r"); // Line 2
char c; // Line 3
c=fgetc(fp); // Line 4
while(c!=EOF) // Line 5
{
printf("%c",c); // Line 6
c=fgetc(fp); // Line 7
}
fclose(fp); // Line 8
}
Description
Line 1 to Line 4 is same as in the above example. The difference starts from line no. 5 where we have written a while loop. The condition inside the loop checks if the value of "c" is not equal to end of file (EOF). The loop continues to read the file char by char till the end of file. How does the program know that this is the end of file and no further reading is required? When we create a file the OS places an invisible mark at the end of the file called the "end of file marker". This marker denotes that end of the file. The loop is searching for this marker so that it can print the file data from the beginning till the end. The "fgetc()" has been written twice once outside the loop and once inside the loop. Why twice ? If you don't write it inside the loop, the value of "c" will struck to "a"(the first char) of the file. What we want to do is to read the whole file, so we should write it inside the loop also so that it can move from a to b to c till the end of the file. This is how it works, assuming that the file has the data (without the quotes)
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
Line 4 reads the first char "a", since "a" is not the end of the file, the control enters the loop. The value of c which is "a" is printed. Line 7 reads the second char which is "b", since "b" also is not end of the file the loop continues to print the value of c which is "b". Line 7 again reads the next char which is "c", "c" is printed and the program continues. Once "z" is read the compiler makes an attempt to read the next char but all the data has already been read. So the next char is the "end of file marker". The condition becomes false and the file is closed using the "fclose()". Closing a file is optional but it is a good practice to close a file to free the resources.
0 comments:
Post a Comment