08.01.2023 Views

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

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

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

Chapter 5 ■ Programs with Repetition Logic

• The numbers in the file could be supplied in “free format”—any amount

could be put on a line. For example, the sample data could have been typed

on one line as above or as follows:

24 13

55 32

19 0

or like this:

24 13

55

32 19

0

or like this:

24

13

55

32

19

0

• As an exercise, add statements to the program so that it also prints the

largest and smallest numbers in the file.

■■File cannot be found when you try to run this program, it may not run properly because it cannot find the

file input.txt. This may be because the compiler is looking for the file in the wrong place. Some compilers

expect to find the file in the same folder/directory as the program file. Others expect to find it in the same

folder/directory as the compiler. Try placing input.txt in each of these folders, in turn, and run the program.

If this does not work, then you will need to specify the complete path to the file in the fopen statement. For

example, if the file is in the folder data that is in the folder CS10E, which is on the C: drive, you will need to use

the following statement:

FILE * in = fopen("C:\\CS10E\\data\\input.txt", "r");

5.9 Send Output to a File

So far, our programs have read data from the standard input (the keyboard) and sent output to the

standard output (the screen). We have just seen how to read data from a file. We now show you

how you can send output to a file.

This is important because when we send output to the screen, it is lost when we exit the

program or when we switch off the computer. If we need to save our output, we must write it to a

file. Then the output is available as long as we wish to keep the file.

110

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!