Pages

Thursday, December 2, 2010

Reading a file string by string

The "fgetc()" function is fine for printing text files but as discussed earlier it will take a long time to process a large file using "fgetc" since it can read only a char to at a time. Therefore in order to read a large file we can make use of the "fgets()" . As the name suggest "s" in fgets() is for string. It can read a string from a file. How many chars will be read will entirely be your choice.

Syntax
    fgets(char[],int bytes,filepointer);

The first argument (char[]) is the string that will hold the chars that have been read from the file. The second argument is the number of bytes that we want to read from the file in one go. The third argument is the file pointer from where we want to read.

Example
    char str[10];
    fgets(str,10,fp);


    In this example, fgets() will read 10 chars(bytes) from the file pointed by "fp" and will store it in the string "str". Keep an eye on the size of the array and the number of bytes that we are reading. The array should be large enough to accommodate the read chars. If we are reading 20 chars from the file and the size of the array
is 10, it won't be able to store the data. If the case is just opposite, the array is large and we are reading less bytes the program will work fine but it will be a memory wastage.

Example - To read a file string by string
#include
#include
void main()
{

    FILE *fptr; // Line 1
    fptr=fopen("anyfile.txt","r"); // Line 2
    char str[15]; // Line 3
    fgets(str,15,fptr); // Line 4
    printf("\nFollowing is the data of the file"); // Line 5
    printf("%s",str); // Line 6
    fclose(fptr); // Line 7
}

Description
Line 3
    Declares a string named "str" with a size of 15 chars. The size of the array should be carefully chosen, it should not be too large or too small. When the data will be read from the file, this is the place where the date will be placed.
Line 4
    The fgets() will read the first 15 chars of the file pointed by fptr and place them in the array. If the file has more than 15 chars, the remaining chars will be ignored. It is also worth to note that the "arrangement" of text inside the file is very important here. If the 15 chars are not in one line the "fgets()" will read the data only up to the point where it meets a "enter key" or "\n". We have asked "fgets()" to read the first 15 chars,but if there is an "enter" before the 15 char can be read, "fgets()" will not read further.

Consider the following two files

File 1 has the data
abcdefghijklmnopqrstuvwxyz

the above code will read the first 15 chars and the output shown will be
abcdefghijklmno

File 2 has the same data but is spread in multiple lines
abc
defghij
k
lm
nopqrstuv
wxy
z


the above code will print only "abc" since all chars are not in a single line. The compiler will encounter "\n" after reading the alphabet "c", therefore further reading will be discontinued. So you can say that writing 15 or any other number as the second argument does not guarantee that 15 chars will be read. How many char will be printed depends on how the data is stored in the file. If the data is in the form of the second file then writing 15 will not help. For reading the second file a string of 9 will also work same as a string of 15 since the longest line in the file(nopqrstuv) has 9 chars. A smaller string will also help in saving memory.


Example 2 - Reading the whole file string by string.
#include
#include
void main()
{

    FILE *fptr; // Line 1
    fptr=fopen("goodfile.txt","r"); // Line 2
    char str[15]; // Line 3
    while(fgets(str,15,fptr)!=NULL)) // Line 4
        printf("%s",str); // Line 5
    fclose(fptr); // Line 6
}

Description
Line 4
    In line number 4, the "fgets()" has been written inside a loop. The loop will read 15 chars at a  time and will continue till it reaches the end of the file. If the file has 150 chars, how many times the loop will run? 15 times? No, it all depends on how the data is saved in the file. If all the 150 chars are written one char per line, the loop will run 150 times, and if all the chars are written in a stretch then the loop will take 15 cycles to process the whole file.
Line 5
    The data in "str" will be printed in line no. 5, first the first 15 chars, then the next 15 chars till the whole file is finished.

0 comments:

Post a Comment