The console function like printf() and scanf() have
been used for input/output .This scheme is adequate if the volume of
data involved in not so large or it is not necessary to store the
information for further use. However, many applications may require a
large amount of data to be read, processed, and also saved for later
use. Such information is stored on the auxiliary memory device in the
form of data file.And a file is a collection of bytes that is given a
name. In most computer systems, files are used as a unit of storage
primarily on floppy-disk or fixed-disk data storage system (or they can
be CDs or other storage device). Thus data files allow us to store
information permanently, and to access and alter that information
whenever necessary. The file handling function available in standard
library in order to implement I/O midel is classified as follows:
a) File access
b) Operation input/output
c) Formatted input/output
d) Character input/output
e) Direct input/output
f) File positioning
g) Error handling
The
file access included the function like fopen() to open a file, fclose()
to close a file , fflush () to flush out the buffer associated with a
file, and freopen() to change the file associated with a stream. Also
setvbuf() and setbuf() functions are use to allow the users explicitly
control the file buffering strategy. The operation on file includes like
remove() to remove a file, rename() to rename a file ,tempfile() to
create a temporary binary file and tmpnam() to generate a unique
filename. Formatted input/output group includes the n functions
fscanf(), scanf() and sscanf() to read formatted data. Similarly
fprintf() ,printf(), sprint(), vfprintf(), vprintf() and vsprintf() to
write formatted data. The character input/output group includes the
functions fgetc() ,getc() and getchar() to read a character from an
input stream and functions ungetc() to push back a character to an input
stream. The functions fgets() and gets() are to read strings and the
output functions fputc(),putc(), putchar(), fputs() and puts() are also
included in this group. The direct input/output group includes functions
fread() to read and fwrite() to write a certain number of data items
specified size. File positioning group includes functions fread() to
read and fwrite() to write a certain number if data items specified
size. File positioning group includes functions to set the file positon
to some specified value to allow access to a specific portion of the
seek(),interrogate the current file position ftell(),and reset the file
position to the beginning of the file rewind(). Error handling group
include functions to test whether EOF returned by a function indicates
an end-of-file or an error (feof and ferror), clear end-of-file and
indicators clearer, and map the error number errno to an error message
perror.
File Accessing:-
To
work with file using any file handling library functions, C requires a
pointer to structure FILE defined in <stdio.h>.The declaration of
file pointer is done as follows:
FILE *fptr;
This
declaration creates a variable fptr, which is pointed to the FILE
structure. The pointer to the FILE structure is also called file
pointer. The FILE structure describes the current state of a file such
as file status flag, file descriptor, file buffer etc. There are also
predefined file pointers such stdin, stdout, and stderr which refers to
standard input (normally keyboard), standard output (normally monitor)
and standard error (connected to screen for error handling). The file
access functions provide the facilities to open and close a file, flush
out the file buffer, change the file content and control the file
buffering strategy. Before working with a file it should be opened
first. To open a file and associate it with a stream, we use fopen().Its
prototype is shown here:
FILE *fopen(char *fname,char *mode); The fopen() function, like all the file-system functions ,uses the header <stdio.h> .The name of the file including the path is specified by fname and the mode specifies how file is accessed. Both of these parameters are string. The string specifying the mode is shown in following table.
S.N | Mode | Meaning |
1 | "r" | Open a text file for reading |
2 | "w" | Create a text file for writing |
3 | "a" | Append to a text file |
4 | "rb" | Open a binary file for reading |
5 | "wb" | Open a binary file for writing |
6 | "ab" | Append to binary file |
7 | "r+" | Open a text file for read/write |
8 | "w+" | Create a text file for read/write |
9 | "a+" | Append or creat a text file for read/write |
10 | "r+b" | Open a binary file for read/write |
11 | "w+b" | Create a binary file for read/write |
12 | "a+b" | Append a binary file for read/write |
No comments:
Post a Comment
Comment