A file represents a sequence of bytes, regardless of it being a text file or a binary file.
When the I/O terminal is used the entire data is lost if the
program is terminated or the computer is being turned off. So it is a
compulsion for storing the data on a permanent device. A collection of data
which is stored on a secondary device like a hard disk is known as a file.
A file is generally used as real-life applications that
contain a large amount of data.
C programming language provides access on high level
functions as well as low level (OS level) calls to handle file on your storage
devices.
A text file is a collection of characters. Binary file is a
collection of bytes.
USING FILES IN C
To use a file four essential actions should to be carried
out.
These are :
·
Declare a file pointer variable.
·
Open a file using the fopen() function.
·
Process the file using suitable functions.
·
Close the file using the fclose() function.
Declaration of File Pointer
Files may be used in a program, when reading or writing, the
type of file that is to be used must be specified. This is accomplished by
using a variable called a file pointer, a pointer variable that points to a
structure FILE. FILE is a structure declared in stdio.h. The members of the
FILE structure are used by the program in various file access operations.
File pointer is used which is declared as
FILE *filePointer;
Opening Files
The fopen( ) function
to create a new file or to open an existing file. This call will initialize an
object of the type FILE,
which contains all the information necessary to control the stream. The
prototype of this function call is as follows –
FILE *fopen( const char *
filename, const char * mode );
filename is a string
literal, which is used to name file, and
access mode can have one of the following values –
- “r” – Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer which points to the first
character in it. If the file cannot be opened fopen( ) returns NULL.
- “w” – Searches file. If the file exists, its contents are
overwritten. If the file doesn’t exist, a new file is created. Returns
NULL, if unable to open file.
- “a” – Searches file. If the file is opened successfully fopen( ) loads
it into memory and sets up a pointer that points to the last character in
it. If the file doesn’t exist, a new file is created. Returns NULL, if
unable to open file.
- “r+” – Searches file. If is opened successfully fopen( ) loads it
into memory and sets up a pointer which points to the first character in
it. Returns NULL, if unable to open the file.
- “w+” – Searches file. If the file exists, its contents are
overwritten. If the file doesn’t exist a new file is created. Returns
NULL, if unable to open file.
- “a+” – Searches file. If the file is opened successfully fopen( )
loads it into memory and sets up a pointer which points to the last
character in it. If the file doesn’t exist, a new file is created. Returns
NULL, if unable to open file.
- “rb” - Open a binary file for reading
- “wb” - Open a binary file for writing
- “ab” - Append to a binary file
- “r+b” - Open a binary fi le for read/write
- “w+b” - Create a binary file for read/write
- “a+b” - Append a binary file for read/write
If File pointer is used which is declared as
FILE *filePointer;
So, the file can be opened as
filePointer = fopen(“fileName.txt”, “w”)
Reading from a file –
The file read operations can be performed using functions
fscanf or fgets. Both the functions performed the same operations as that of
scanf and gets but with an additional parameter, the file pointer. So, it
depends on how to read the file - line by line or character by character.
Ex:
FILE * filePointer;
filePointer = fopen(“fileName.txt”, “r”);
fscanf(filePointer, "%s %s %s %d", str1, str2,
str3, &year);
Writing a file –
The file write operations can be perfomed by the functions
fprintf and fputs with similarities to read operations.
Ex:
FILE *filePointer ;
filePointer = fopen(“fileName.txt”, “w”);
fprintf(filePointer, "%s %s %s %d", "We",
"are", "in", 2012);
Closing a file –
After every successful file operation, always close a file.
For closing a file, fclose function is used.
Ex:
filePointer= fopen(“fileName.txt”, “w”);
---------- Some file Operations -------
fclose(filePointer)
No comments:
Post a Comment