FILE MANAGEMENT IN C


FILE MANAGEMENT IN C:



INTRODUCTION:

A file is a place on the disk where a group of related data is stored. C supports a number of functions that have the ability to perform basic file operations, which include:

1.   naming a file,
2.    opening a file
3.    reading data from a file
4.    writing data to a file
5.     closing a file

The C library uses the following High Level I/O functions:

Function name                              Operation
fopen()                                        Creates a new file for use
                                                    Opens an existing file for use
fclose()                                       Closes a file which has been opend for use
getc()                                          Reads a character from a file
putc()                                         Writes a set of data values to a file
printf()                                      Writes a set of values from a file
fscanf()                                       Reads a set of data values from a file
getw()                                         Reads an integer to a file
putw()                                         Writes an integer to a file
fseek()                                         Sets the position to a desired point in the file
ftell()                                           Gives the current position in the file
                                                     (in terms of bytes from the start)
rewind()                                      Sets the position to the beginning of the file.

Opening a file:
Before we can write information to a file on a disk or read it, we must open the file. Opening a file establishes a link between the program and the operating system. We provide the operating system with the name of the file and whether we plan to read or write to it. The link between our program and the operating system is a structure called FILE which has been defined in the header file “stdio.h” (standing for standard input/output header file). When we request the operating system to open a file, we attach a pointer to the Structure

FILE. That is why, we make the following declaration before opening the file,

                                         FILE *fp;

Each file we open will have its own FILE structure. The FILE structure contains information about the file being used, such as its current size, its location in memory etc. More importantly it contains a character pointer which points to the character that is about to get read. Now let us understand the following statements,
                                           FILE *fp;
                                    fp = fopen(“pr1.c”, “r”);

fp is a pointer variable, which contains address of the structure FILE ,which has been defined in the header file “stdio.h”.

fopen() will open a file “pr1.c” in ‘read’ mode, which tells the C compiler that we would be reading the contents of the file. Note that “r” is a string and not a character; hence the double quotes and not single quotes. In fact, fopen() performs three important tasks when you open the file in “r” mode:

1. Firstly it searches on the disk the file to be opened.
2. If the file is present, it loads the file from the disk into memory.
If the file is absent, fopen() returns a NULL. NULL is a macro defind in “stdio.h” which indicates that you failed to open the file.
3.It sets up a character pointer which points to the first character ofthe memory where the file has been loaded.

Reading from a file:

Once the file has been opened for reading using fopen(), as we have seen the file’s contents are brought into memory and a pointer points to the very first character. The read the file’s contents from memory there exists a

function called fgetc(). This has been used in our sample program through,

                                                c = fgetc(fp);

fgetc() reads the character from current pointer position, advances the pointer position so that it now points to the next character, and returns the character that is read, which we collected in the variable c.



We have the used the function fgetc() within an indefinite while loop. There has to be a way to break out of this while. There should be a break whenever we reach the end of the file. End of the file is signified by a special

character, whose ascii value is 26. This character is inserted beyond the last character in the file, when it is created. This character can also be generated from the keyboard by typing ctrl Z. When reading from the file, when fgetc() encounters this special character, instead of returning the character that it has read, it returns the macro EOF. The EOF macro has been defind in the file “stdio.h”.


Trouble in opening a file:

There is a possibility that when we try to open a file using the function fopen(), the file may not be opened. While opening the file in “r” mode, this may happen because the file being opened may not be present on the disk at all. And you obviously cannot read a file which doesn’t exist. Similarly, while opening the file for writing fopen() may fail due to a number of reasons, like, disk space may be insufficient to open a new file, or the disk may be write

protected and so on.



#include “stdio.h”
main()
{
FILE *fp;
fp = fopen(“pri.c”, “r”);
if(fp == NULL)
{
puts(“Cannot open file”);
exit();
}
}

Closing the file:

When we have finished reading from the file, we need to close it. This is done using the function fclose() through the statement,

fclose(fp);

File opening modes:

A file can be opened in one of several modes. The tasks performed byfopen() when a file is opened in each of these modes are also mentioned. “r” Searces file. If the file exists, loads it into memory and sets up a pointer which points to the first character in it. If the file does’t exist it returns

NULL.

Operations possible - reading from the file.

w” Searches file. If the file exits, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open
file.

Operations possible - writing to a file.

a” Searches file. If the file exists, loads it into memory and sets up a pointerwhich points to the first character in it. If the file doesn’t exist, a new file

is created. Returns NULL, if uanble to open file.


Operations possible - appending new contents at the end of file.

r+” Serches file. If the file exists, loads it into memory and sets up a pointer which points to the first character in it If file doesn’t exist it returns

NULL.


Operations possible - reading existing contents, writing new contents, modifying existing contents of the file.

w+” Serches file. If the file exists, its contents are destroyed. If the file doesn’t exists a new file is created. Returns NULL, if uanble to open

file.


Operation possible - writing new contents, reading them back and modifying existing contents of the file.

a+” Searches file. If the file exists, loads it into memory and sets up a pointer which points to the first character in it. If the file doen’t exists, a new file is created. Returns NULL, if unable to open file.



Operations possible - reading existing contents, appending new  contents to end of file. Cannot modify existing contents.


DEFINING AND OPENING A FILE


If we want to store data in a file in the secondary memory, we must specify certain things about the file, to the operating system. They include:

1. Filename
2. Data structure
3. Purpose


Filename is a string of characters that make up a valid filename for the operating system. It may contain two parts, a primary name and an optional period with the extension. Examples:

input.data
store
prog.c
student.c
text.out

Data structure of a file is defined as FILE in the library of standard I/O function definitions. Therefore, all files should be declared as type FILE before they are used. FILE is a defined data type.

When we open a file, we must specify what we want to do with the file.For example, we may write data to the file or read the already existing data. Following is the general format for declaring and opening a file:

FILE *fp;

fp = fopen(“filename”, “mode”);

The first statement declares the variable fp as a “pointer to the data type FILE”. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer which contains all the information about the file and it links between the system and the program. The second statement also specifies the purpose of opening this file. The mode does this job. Mode can be one of the following:

              “r”           opens the file for readng only

              “w”         open the file for writing only

              “a “         open the file for appending (or adding) data to it.

Note that both the filename and mode are specified as strings. They should be enclosed in double quotaton marks.



Consider the following statements:

FILE *pt1, *pt2;

pt1 = fopen(“data”, “r”);

pt2 = fopen(“results”, “w”);

The file data is opend for reading and results is opend for writing. In case, the results file already exits, its contents are deleted and the file is opened as a new file. If data file does not exit, an error will occur.



CLOSING A FILE



A file must be closed as soon as all operations on it have been completed. This ensures that all outstanding information associated with thefile is flushed out from the buffers and all links to the file are broken. Look at the following segment of a program to close the opend files.

...........

FILE *pt1, *pt2;

pt1 = fopen(“input”, “w”);

pt2 = fopen(“ouput”, “r”);
.......
fclose(pt1);
fclose(pt2);
.........

This program opens two files and closes them after all operations onthem are completed. Once a file is closed, its file pointer can be reused for another file.

INPUT/OUTPUT OPERATIONS ON FILES

Once a file is opend, reading out of or writing to it is accomplished using the standard I/O routines.
To create a new file:


In this program we can write strings to a file using the function fputs().
#include <stdio.h>
main()
{
FILE *fp;
char s[80];
fp = fopen(“neha.txt”, “w”);
if(fp == NULL)
{
puts(“Cannot open file”);
exit();
}
printf(“\nEnter a few lines of text:\n”);
printf(“Press Enter key at the end of the line\n”);
printf(“Press two times Enter key to close the file\n”);
while(strlen(gets(s)) > 0)
{
fputs(s, fp);
fputs(“\n”, fp);
}f
close(fp);
}
Note that each string is terminated by hitting enter. To terminate the execution of the program, hit enter at the beginning f a line. This creates a string of zero length, which the program recognises as the signal to close the
file and exit.



Explanation: We have set up a character array to receive the string; the fputs() function then writes the contents of the array to the disk. Since fputs() does not automatically add a newline character to the end of the string, we must do this explicitly to make it easier to read the string back from the file.



To copy the contents of a file into another file:



In this program we can copy the contents of one text file into another text file, character by character.

#include <stdio.h>
main()
{
FILE *fs, *ft;
char c;
fs = fopen(“pr1.c”, “r”);
if(fs == NULL)
{
puts(“Cannot open source file”);
exit();
}f
t = fopen(“pr2.c”, “w”);
if(ft == NULL)
{
puts(“Cannot open targer file”);
fclose(fs);
exit();
}
while(1)
{
c = fgetc(fs);
if(c == EOF);
break;
else
fputc(c, ft);
}f
close(fs);
fclose(ft);
}
Explanation: In the above program fgetc() function is used to reads characters from a file and fputc() function is used to write characters to a file. The ft and fs are pointers for target file and source file. The fclose() is used
to close the file. It clears any characters remaining in the buffers.

To read strings from the file and displays them on screen:

#include <stdio.h>
main()
{
FILE *fp;
char s[80];
clrscr();
fp = fopen(“suhail.txt”, “r”);
if(fp == NULL)
{
puts(“Cannot open file”);
exit();
}
while(fgets(s, 79, fp) != NULL)
printf(“%s”, s);
fclose(fp);
getch();
}
Explanation:
The function fgets() takes three arguments. The first is the address where the string is stored, and the second is the maximum length of the string. This argument prevents fgets() from reading in too long a string and overflowing the array. This third argument, as usual, is the pointer to the  structure FILE. When all the lines from the file have been read, we attempt to read one more line, in which case fgets() returns a NULL.


Formatted Disk I/O Functions:



For formatted reading and writing of characters, strings, integers, floats,  there exists two functions, fscanf() and fprintf().

#include <stdio.h>
main()
{
FILE *fp;
char another = ‘Y’;
char name[40];
int age;
float bs;
clrscr();
fp = fopen(“employee.dat”, “w”);
if(fp == NULL)
{
puts(“Cannot open file”);
exit();
}
while(another == ‘Y’)
{
printf(“\nEnter name, age and basic salary\n”);
scanf(“%s %d %f”, name, &age, &bs);
fprintf(fp, “%s %d %f\n”, name, age, bs);
printf(“Another employee(Y/N)”);
fflush(stdin);
another = getche();
}f
close(fp);
getch();
}

Explanation:

The key to this program is the function fprintf(), which writes the values of three variables to the file. This function is similar to printf(), except that a FILE pointer is included as the first argument. As in printf(), we can format the

data in a variety of ways, by using fprintf(). In fact all the format conventions of printf() function work with fprintf() as well. We have used fflush() function. The reason is to get rid of a pecularity of scanf(). After supplying data for one employee, we would hit the enter key. What scanf() does is it assign name, age and salary to appropriate variables and keeps the enter key unread in the keyboard buffer. So when it’s time to supply Y or N for the question ‘Another employee (Y/N)’ getche() will read the enter key from the buffer thinking that user has entered the enter key. To avoid this problem we use the function fflush(). It is designed to remove or ‘flush out’ any

data remaining in the buffer. The argument to fflush() must be the buffer which we want to flush out. Here we use ‘stdin’, which means buffer related with standard input device the keyboard.



To read formatted file and displays them on screen:

#include <stdio.h>
main()
{
FILE *fp;
char name[40];
int age;
float bs;
clrscr();
fp = fopen(“employee.dat”, “r”);
if(fp == NULL)
{
puts(“Cannot open file”);
exit();
}
while(fscanf(fp, “%s %d %f”, name, &age, &bs) != EOF)
printf(“\n%s %d %f”, name, age, bs);
fclose(fp);
getch();
}
Explanation:

This program uses the fscanf() function to read the data from the disk. This function is similar to scanf(), except that, as with fprintf(), a pointer to FILE is included as the first argument.



To write records to a file using structure:

#include <stdio.h>
main()
{
FILE *fp;
char another = ‘Y’;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp = fopen(“employee.dat”, “w”);
if(fp == NULL)
{
puts(“Cannot open file”);
exit();
}
while(another == ‘Y’)
{
printf(“\nEnter name, age and basic salary:”);
scanf(“%s %d %f”, e.name, &e.age, &e.bs);
fprintf(fp, “%s %d %.2f\n”, e.name, e.age, e.bs);
printf(“Add another record (Y/N)”);
fflush(stdin);
another = getche();
}f
close(fp);
getch();
}
Note: In this format numbers would occupy more number of bytes, since the
file is opend in text mode.


To read records from  a file using structure:

#include <stdio.h>
main()
{
FILE *fp;
struct emp
{
char name[40];
int age;
float bs;
};
struct emp e;
fp = fopen(“employee.dat”, “r”);
if(fp == NULL)
{
puts(“Cannot open file”);
exits();
}
while(fscanf(fp, “%s %d %f”, e.name, &e.age, &e.bs) != EOF)
printf(“\n%s %d %.2f”, e.name, e.age, e.bs);
fclose(fp);
getch();
}



The getw and putw Functions:

The getw and putw are integer-oriented functions. They are similar to the getc and putc functions and are used to read and write integer values. These functions would be useful when we deal with only integer data. The

general forms of getw and putw are:

                                   getw(fp);
                                putw(integer, fp);

Example:

A file named data contains a series of integer numbers. Code a program to read these numbers and then write all odd numbers to a file to be  called odd and all even numbers to a file to be called even. In this programwe use three files simultaneously and therefore we need to define three file pointers f1, f2 and f3.

#include <stdio.h>
main()
{
FILE *f1, *f2, *f3;
int number, i;
printf(“Contents of data file\n\n”);
f1 = fopen(“data”, “w”);
for(i = 1; i <= 30; i++)
{
scanf(“%d”, &number);
if(number == -1)
break;
putw(number, f1);
}f
close(f1);
f1 = fopen(“data”, “r”);
f2 = fopen(“odd”, “w”);
f3 = fopen(“even”, “w”);
while( (number = getw(f1) ) != EOF)
{
if(number %2 == 0)
putw(number, f3);
else
putw(number, f2);
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen(“odd”, “r”);
f3 = fopen(“even”, “r”);
printf(“\n\nContents of odd file\n\n”);
while( (number = getw(f2) ) != EOF)
printf(“%4d”, number);
printf(“\n\nContents of even file\n\n”);
while( (number = getw(f3) != EOF)
printf(“%4d”, number);
fclose(f2);
fclose(f3);
}

Comments