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.

360 Chapter 16 Input and Output Operations <strong>in</strong> C<br />

displayed by pr<strong>in</strong>tf do not appear <strong>in</strong> your w<strong>in</strong>dow but are <strong>in</strong>stead written <strong>in</strong>to the file<br />

called data.<br />

To see how this works, type <strong>in</strong> the very first program you wrote, Program 3.1, and<br />

compile the program <strong>in</strong> the usual way. Now execute the program as you normally would<br />

by typ<strong>in</strong>g <strong>in</strong> the program name (assume it’s called prog1):<br />

prog1<br />

If all goes well, you should get the output<br />

<strong>Programm<strong>in</strong>g</strong> is fun.<br />

displayed <strong>in</strong> your w<strong>in</strong>dow. Now type <strong>in</strong> the follow<strong>in</strong>g command:<br />

prog1 > data<br />

This time, notice that you did not get any output at the term<strong>in</strong>al.This is because the<br />

output was redirected <strong>in</strong>to the file called data. If you now exam<strong>in</strong>e the contents of the<br />

file data,you should f<strong>in</strong>d that it conta<strong>in</strong>s the follow<strong>in</strong>g l<strong>in</strong>e of text:<br />

<strong>Programm<strong>in</strong>g</strong> is fun.<br />

This verifies that the output from the program went <strong>in</strong>to the file data as described previously.You<br />

might want to try the preced<strong>in</strong>g sequence of commands with a program that<br />

produces more l<strong>in</strong>es of output to verify that the preced<strong>in</strong>g process works properly <strong>in</strong><br />

such cases.<br />

You can do a similar type of redirection for the <strong>in</strong>put to your programs. Any call to a<br />

function that normally reads data from your w<strong>in</strong>dow, such as scanf and getchar, can be<br />

easily made to read its <strong>in</strong>formation from a file. Program 5.8 was designed to reverse the<br />

digits of a number.The program uses scanf to read <strong>in</strong> the value of the number to be<br />

reversed from the term<strong>in</strong>al.You can have the program <strong>in</strong>stead get its <strong>in</strong>put from a file<br />

called number,for example, by redirect<strong>in</strong>g the <strong>in</strong>put to the program when the program is<br />

executed. If the program is called reverse, the follow<strong>in</strong>g command l<strong>in</strong>e should do the<br />

trick:<br />

reverse < number<br />

If you type the number 2001 <strong>in</strong>to a file called number before issu<strong>in</strong>g the preced<strong>in</strong>g command,<br />

the follow<strong>in</strong>g output appears at the term<strong>in</strong>al after this command is entered:<br />

Enter your number.<br />

1002<br />

Notice that the program requested that a number be entered but did not wait for you to<br />

type <strong>in</strong> a number.This is because the <strong>in</strong>put to the program—but not its output—was<br />

redirected to the file called number.Therefore, the scanf call from the program had the<br />

effect of read<strong>in</strong>g the value from the file number and not from your term<strong>in</strong>al w<strong>in</strong>dow.The<br />

<strong>in</strong>formation must be entered <strong>in</strong> the file the same way that it would be typed <strong>in</strong> from the

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

Saved successfully!

Ooh no, something went wrong!