written 6.0 years ago by | • modified 6.0 years ago |
Subject : Structured Programming Approach
Title : Pointers and Files
Marks : 10M
written 6.0 years ago by | • modified 6.0 years ago |
Subject : Structured Programming Approach
Title : Pointers and Files
Marks : 10M
written 6.0 years ago by |
It becomes cumbersome and time consuming to handle large volumes of data through terminals.
The entire data is lost when either the program is terminated or the computer is turned-off. It is therefore necessary to have a more flexible approach where data can be stored on the disk and read whenever necessary, without destroying the data. This method employs the concept of files to store data.
fopen( ) # Creates a new file for use .
fclose( ) # Closes a file which has been opened for use.
getc( ) # Reads a character from a file.
putc( ) # Writes a character to a file .
fprintf( ) # Writes a set of data values to a file.
fscanf( ) # Reads a set of data values from a file.
getw( ) # Reads an integer from a file.
putw( ) # Writes an integer to a file.
fseek( ) # Sets the position to the 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.
Defining And Opening A File
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. The following is the general format for declaring and opening a file:
FILE *fp;
fp = fopen(“filename”, “mode”);
Mode can be one of the following:
r open the file for reading only.
w open the file for writing only.
a open the file for appending(or adding)data to it.
The additional modes of operation are:
r+ the existing file is opened to the beginning for both reading and writing.
w+ same as w except both for reading and writing.
a+ same as a except both for reading and writing.
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 the file is flushed out from the buffers and all links to the file are broken. It also prevents any accidental misuse of the file. The syntax for closing a file is
fclose( file_pointer);
This would close the file associated with the FILE pointer file_pointer .
Example:
…..
…..
FILE *p1, *p2;
p1 = fopen(“INPUT”, “w”);
p2 = fopen(“OUTPUT” , “r”);
…..
…..
fclose(p1);
fclose(p2);
…..
This program opens two files and closes them after all operations on them are completed. Once a file is closed, its file pointer can be reused for another file. All files are closed automatically whenever a program terminates.
Input/Output Operations On Files
The file i/o functions getc and putc are similar to getchar and putchar functions and handle one character at a time. The statement
putc(c, fp1);
writes the character contained in the character variable c to the file associated with FILE pointer fp1. Similarly, getc is used to read a character from a file that has been opened in the read mode. For example, the statement
c = getc(fp2);
would read a character from the file whose file pointer is fp2.