Introduction
File Pointer:
A file pointer is a pointer to a structure, which contains information about the file, including its name, current position of
the file, whether the file is being read or written, and whether errors or end of the file have occurred. The user does not need to
Know the details, because the definitions obtained from stdio.h include a structure declaration called FILE. The only declaration
needed for a file pointer is symbolized by
FILE *fp;This says that fp is the file pointer that points to a FILE structure.
Opening a file:
The fopen() function opens a stream for use and links a file with that stream. A file pointer associated with that file is then
returned by the fopen() function. Most often the file is a disk file. The prototype for the fopen() function is
FILE *fopen(const char* filename, const char *mode);Where filename is a pointer to a string of characters that make up a valid file name and can also include a path specification. Another parameter is a pointer to a string, name mode, determines how the file can be opened. The below table shows the valid modes in which a file may be opened. Strings like “w + b” can also be represented as “wb+”.
Mode | Meaning |
---|---|
r | Open a text file reading |
w | Create a text file writing |
a | Append to a text file |
rb | Open a binary file for reading |
wb | Create a binary file for writing |
ab | Append to a binary file |
r+ | Open a text file read/write |
w+ | Create a text file for read or write |
a+f | Append or create a text file for read/write |
r+b | Open a binary file for read/write |
w+b | Create a binary file for read/write |
a+b | Append a binary for read/write |
As can be seen from above table, the files can be opened in the text or the binary mode. If an error occurs when the fopen()
function is opening a file, it returns a null pointer.
Consider the following code snippet:
FILE *fp; fp=fopen("try.c","r");fopen() opens the file “try.c” in read mode. When a file is opened in read mode, three important tasks are performed by fopen().
1. A search is carried out on the disk for the file to be opened.
2. If the file is found, it is loaded into memory from the disk. In case the file is too large, then the file is loaded partwise. In case the file is not found, a NULL() is returned by fopen(). “stdio.h” contains a macro defined as NULL, which indicates that the attempt to open the file failed.
3. fopen() then open sets up a character pointer. The character pointer is a part of FILE structure and points to the first character in memory where the file is loaded.
If a file xyz is to be opened for writing, the code for it would be:
FILE *fp; fp=fopen("xyz","w");However, a file is generally opened using the following statements.
FILE *fp; If((fp=fopen (“xyz”,”w”))==NULL) { Printf(“cannot open Files”); Exit(1); }The macro NULL is defined in stdio.h as ’\0’. If a file is opened using the above method, fopen() detects any error in opening a file, such as a write-protected or a full disk, before attempting to write to it. A null is used to indicate failure because no file pointer will ever have that value.
If a file is opened foe writing, any pre-exiting file with that name will be overwritten. This is because when a file is opened in a write mode, a new file is created. If records have to be added to an existing file, it should be opened with the mode “a”. if a file is opened for read/write operations, it will not be erased if it exists. However, if it does not exist, it will be created.
Closing a File:
As said earlier, there is usually a limit on the number of files that can be opened at one time, and so it is important to close the file once it has been used.
This ensures that various system resources will be free and reduces the risk of overshooting the set limit. The fclose() function closes a stream that was
opened by a call to fopen(). It writes any data still remaining in the disk buffer to the file. The prototype for fclose() is
Fclose (FILE *fp);Where fp is the file pointer. The function fclose() returns an integer value 0 for successful closure, any other value indicates an error. The fclose() generally fails when a disk has been prematurely removed from the drive or there is no more space on the disk.
The other function used for closing streams is the fcloseall() function. This function is useful when many open streams have to be closed at the same time. It closes all open streams and returns the number of streams closed of streams closed or EOF if any error is detected. It can be used in the following manner.
fcl=fcloseall(); if(fcl==EOF) printf(“Error closing files”); else printf(“%d files closed”,fcl);
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment