Skip to main content

File Pointers

A file pointer is a pointer to a structure, which contains information about the file, including its name, current address of the file in the memory, 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("D:\\try.txt","r");
fopen() opens the file “try.txt” present in D drive 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 new.txt is to be created in c for writing, the code for it would be:
FILE *fp;
fp=fopen("c:\\new.txt","w");
However, a file is generally opened using the following statements.
FILE *fp;
If((fp=fopen ("c:\\new.txt","r"))==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);
and simply
fclose(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 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

Popular posts from this blog

Pointers in C

Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool. Actually, it's very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what pointers are and how you use them. What are pointers? Why should you care? Pointers are aptly name: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that you ca...

Types of Computer Networks(LAN,MAN,WAN)

Types of Computer Networks Computer networks are categorized according to: How they are organized physically. The way they are used. The distance over which they operate. Three main types of computer networks are as follows: LAN   (Local Area Network) WAN (Wide Area Network) MAN ( Metropolitan-Area Network) LAN (Local Area Network) LAN is the most common type of network . LAN stands for Local area Network . It covers a small area. Most LANs are used to connect computers in a single building or group of buildings. Hundreds or thousands of computer maybe connected through LAN. Typical LANs can be found in industrial plants, office buildings, and college or university campuses. LANs are capable of transmitting data at very fast rate. Data transmission speeds of LAN are 1 to 100 megabits per second. It is much faster than data transmission over a telephone line. LAN can transmit data in a limited distance. There is also a limit on the number of...

Ring topologies

Ring topologies are similar to bus topologies, except they transmit in one direction only from station to station. Typically, a ring architecture will use separate physical ports and wires for transmit and receive. Token Ring is one example of a network technology that uses a ring topology.