20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Special Functions for Work<strong>in</strong>g with Files<br />

363<br />

Special Functions for Work<strong>in</strong>g with Files<br />

It is very likely that many of the programs you will develop will be able to perform all<br />

their I/O operations us<strong>in</strong>g just the getchar, putchar, scanf,and pr<strong>in</strong>tf functions and<br />

the notion of I/O redirection. However, situations do arise when you need more flexibility<br />

to work with files. For example, you might need to read data from two or more<br />

different files or to write output results <strong>in</strong>to several different files.To handle these situations,<br />

special functions have been designed expressly for work<strong>in</strong>g with files. Several of<br />

these functions are described <strong>in</strong> the follow<strong>in</strong>g sections.<br />

The fopen Function<br />

Before you can beg<strong>in</strong> to do any I/O operations on a file, the file must first be opened.To<br />

open a file, you must specify the name of the file.The system then checks to make certa<strong>in</strong><br />

that this file actually exists and, <strong>in</strong> certa<strong>in</strong> <strong>in</strong>stances, creates the file for you if it does<br />

not.When a file is opened, you must also specify to the system the type of I/O operations<br />

that you <strong>in</strong>tend to perform with the file. If the file is to be used to read <strong>in</strong> data,<br />

you normally open the file <strong>in</strong> read mode. If you want to write data <strong>in</strong>to the file, you open<br />

the file <strong>in</strong> write mode. F<strong>in</strong>ally, if you want to append <strong>in</strong>formation to the end of a file that<br />

already conta<strong>in</strong>s some data, you open the file <strong>in</strong> append mode. In the latter two cases,<br />

write and append mode, if the specified file does not exist on the system, the system creates<br />

the file for you. In the case of read mode, if the file does not exist, an error occurs.<br />

Because a program can have many different files open at the same time, you need a<br />

way to identify a particular file <strong>in</strong> your program when you want to perform some I/O<br />

operation on the file.This is done by means of a file po<strong>in</strong>ter.<br />

The function called fopen <strong>in</strong> the standard library serves the function of open<strong>in</strong>g a file<br />

on the system and of return<strong>in</strong>g a unique file po<strong>in</strong>ter with which to subsequently identify<br />

the file.The function takes two arguments:The first is a character str<strong>in</strong>g specify<strong>in</strong>g the<br />

name of the file to be opened; the second is also a character str<strong>in</strong>g that <strong>in</strong>dicates the<br />

mode <strong>in</strong> which the file is to be opened.The function returns a file po<strong>in</strong>ter that is used<br />

by other library functions to identify the particular file.<br />

If the file cannot be opened for some reason, the function returns the value NULL,<br />

which is def<strong>in</strong>ed <strong>in</strong>side the header file . 3 Also def<strong>in</strong>ed <strong>in</strong> this file is the def<strong>in</strong>ition<br />

of a type called FILE.To store the result returned by the fopen function <strong>in</strong> your<br />

program, you must def<strong>in</strong>e a variable of type “po<strong>in</strong>ter to FILE.”<br />

3. NULL is “officially” def<strong>in</strong>ed <strong>in</strong> the header file ;however, it is most likely also<br />

def<strong>in</strong>ed <strong>in</strong> .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!