08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 ■ Programs with Repetition Logic

We say we have been reading data from the “standard input.” C uses the predefined identifier

stdin to refer to the standard input. When your program starts up, C assumes that stdin refers to

the keyboard. Similarly, the predefined identifier stdout refers to the standard output, the screen.

So far, our programs have written output to the screen.

We can also supply data to a program by storing the data in a file. When the program needs

data, it fetches it directly from the file, without user intervention. Of course, we have to ensure

that the appropriate data has been stored in the file in the correct order and format. This mode

of supplying data is normally referred to as batch mode. (The term batch is historical and comes

from the old days when data had to be “batched” before being submitted for processing.)

For example, suppose we need to supply an item number (int) and a price (double) for

several items. If the program is written assuming that the data file contains several pairs of

numbers (an int constant followed by a double constant), then we must ensure that the data in

the file conforms to this.

Suppose we create a file called input.txt and type data in it. This file is a file of characters or

a text file. Depending on the programming environment provided by your C compiler, it may be

possible to assign stdin to input.txt—we say redirect the standard input to input.txt. Once this

is done, your program will read data from the file rather than the keyboard. Similarly, it may be

possible to redirect the standard output to a file, output.txt, say. If done, your printf’s will write

output to the file, rather than the screen.

We will take a slightly different approach, which is a bit more general since it will work with

any C program and does not depend on the particular compiler or operating system you happen

to be using.

Suppose we want to be able to read data from the file input.txt. The first thing we need to do

is declare an identifier called a “file pointer.” This can be done with the statement.

FILE * in; // read as "file pointer in"

The word FILE must be spelt as shown, with all uppercase letters. The spaces before and after

* may be omitted. So you could write FILE* in, FILE *in or even FILE*in. We have used the

identifier in; any other will do, such as inf, infile, inputFile, payData.

The second thing we must do is associate the file pointer in with the file input.txt and tell C

we will be reading data from the file. This is done using the function fopen, as follows:

in = fopen("input.txt", "r");

This tells C to “open the file input.txt for reading”: "r" indicates reading. (We will use "w"

if we want the file to be opened for “writing,” that is, to receive output.) If we wish, we could

accomplish both things with one statement, thus:

FILE * in = fopen("input.txt", "r");

Once this is done, the “data pointer” will be positioned at the beginning of the file. We can

now write statements that will read data from the file. We will see how shortly.

It is up to us to ensure that the file exists and contains the appropriate data. If not, we will get

an error message such as “File not found.” If we need to, we can specify the path to the file.

Suppose the file is located at C:\testdata\input.txt.

107

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!