Programming in C: Unit V: File processing

Using Files in C

Syntax with Example C Programs

There can be a number of files on the disk. In order to access a particular file, you must specify the name of vistars the file that has to be used.Programming Tip: An error will be generated if you use the filename to access a file rather than the file pointer.

USING FILES IN C

To use files in C, we must follow the steps given below: 

declare a file pointer variable

• open the file

•  process the file

•  close the file

In this section, we will go through all these steps in detail.

Declaring a File Pointer Variable

There can be a number of files on the disk. In order to access a particular file, you must specify the name of vistars the file that has to be used.

Programming Tip: An error will be generated if you use the filename to access a file rather than the file pointer.

This is accomplished by using a file pointer variable that points to a structure FILE (defined in stdio.h). The file pointer will then be used in all subsequent operations in the file. The syntax for declaring a file pointer is

FILE *file_pointer_name;

For example, if we write

FILE *fp;

Then, fp is declared as a file pointer.

Opening a File

A file must first be opened before data can be read from it or written to it. In order to open a file and associate it with a stream, the fopen() function is used. The prototype of fopen() can be given as

FILE *fopen(const char *file_name, const char 26 mode);

Using the above prototype, the file whose pathname is the string pointed to by file_name is opened in the mode specified using the mode. If successful, fopen() returns a pointer-to- structure and if it fails, it returns NULL.

Programming Tip: A file must be opened before any operation can be performed on it.

File Name

Every file on the disk has a name known as the file name. The naming convention of a file varies from one operating system to another. For example, in DOS the file name can have one to eight characters optionally followed by a period and an extension that has one to three characters. However, Windows and UNIX permit filenames having maximum of 256 characters. Windows also lays some restrictions on usage of certain characters in the filenames, i.e., characters such as /,\,:,,?, ", <, >, and ! cannot be part of a file name.

In C, the fopen() may contain the path information instead of specifying the filename. The path gives informa- tion about the location of the file on the disk. If a filename is specified without a path, it is assumed that the file is located in the current working directory. For example, if a file named Student. DAT is located on D drive in directory BCA, then the path of the file can be specified by writing.

D:\BCA\Student. DAT

In C, a backslash character has a special meaning with respect to escape sequences when placed in a string. So in order to represent a backslash character in a C program, you must precede it with another backslash. Hence, the above path will be specified as given below in the C program.

D:\\BCA\\Student. DAT

File Mode

The second argument in the fopen() is the mode. Mode conveys to C the type of processing that will be done with the file. The different modes in which a file can be opened for processing are given in Table 9.1.

Look at the code given below which opens a file using the fopen().

FILE *fp;

Fp = fopen("Student.DAT", "r");

if (fp==NULL)

{

printf("\n The file could not be opened");

exit (1);

}

OR

char filename [30];

FILE *fp;

gets (filename);

fp = fopen(filename, "r+");

if (fp==NULL)

{

printf("\n The file could not be opened");

exit (1);

}

We have already discussed that fopen() returns a pointer to FILE structure if successful and a NULL otherwise.

Programming Tip: An error will be generated if you try to open a file that does not exist.

So it is recommended to check whether the file was successfully opened before actually using the file. The fopen() can fail to open the specified file under certain conditions that are listed below:

• opening a file that is not ready for use

• opening a file that is specified to be on a non-existent directory/drive

• opening a non-existent file for reading

• opening a file to which access is not permitted

Closing a File Using fclose()

To close an open file, the fclose() function is used which disconnects a file pointer from a file. After the fclose() has disconnected the file pointer from the file, the pointer can be used to access a different file or the same file but in a different mode. The fclose() function not only closes the file, but also flushes all the buffers that are maintained for that file.

If you do not close a file after using it, the system closes it automatically when the program exits. However, since there is a limit on the number of files which can be opened simultaneously; the programmer must close a file when it has been used. The prototype of the fclose()function can be given as

int fclose (FILE *fp);

Programming Tip: It is always recommended to close all the opened files when they are not going to be used further in the program.

Here, fp is a file pointer which points to the file that has to be closed. The function returns an integer value which indicates whether the fclose() was successful or not. A zero is returned if the function was successful; and a non- zero value is returned if an error occurred.

Note

When the fclose () is executed, any unwritten buffered data for the stream will be written to the file and any unread buffered data will be discarded.

In addition to fclose(), there is a function fcloseall() which closes all the streams that are currently opened except the standard streams (such as stdin, stdout, and stderr). The prototype of fcloseall () can be given as

int fcloseall (void);

fcloseall() also flushes any stream buffers and returns the number of streams closed.

If a file's buffer has to be flushed without closing it then use fflush() or flushall () to flush the buffers of all open streams.

Programming in C: Unit V: File processing : Tag: : Syntax with Example C Programs - Using Files in C