20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Input and Output Operations with Files<br />

359<br />

The newl<strong>in</strong>e character is repeated outside the brackets so that scanf matches it and does<br />

not read it the next time it’s called. (Remember, scanf always cont<strong>in</strong>ues read<strong>in</strong>g from<br />

the character that term<strong>in</strong>ated its last call.)<br />

When a value is read that does not match a value expected by scanf (for example,<br />

typ<strong>in</strong>g <strong>in</strong> the character x when an <strong>in</strong>teger is expected), scanf does not read any further<br />

items from the <strong>in</strong>put and immediately returns. Because the function returns the number<br />

of items that were successfully read and assigned to variables <strong>in</strong> your program, this value<br />

can be tested to determ<strong>in</strong>e if any errors occurred on the <strong>in</strong>put. For example, the call<br />

if ( scanf ("%i %f %i", &i, &f, &l) != 3 )<br />

pr<strong>in</strong>tf ("Error on <strong>in</strong>put\n");<br />

tests to make certa<strong>in</strong> that scanf successfully read and assigned three values. If not, an<br />

appropriate message is displayed.<br />

Remember, the return value from scanf <strong>in</strong>dicates the number of values read and<br />

assigned, so the call<br />

scanf ("%i %*d %i", &i1, &i3)<br />

returns 2 when successful and not 3 because you are read<strong>in</strong>g and assign<strong>in</strong>g two <strong>in</strong>tegers<br />

(skipp<strong>in</strong>g one <strong>in</strong> between). Note also that the use of %n (to obta<strong>in</strong> the number of characters<br />

read so far) does not get <strong>in</strong>cluded <strong>in</strong> the value returned by scanf.<br />

Experiment with the various formatt<strong>in</strong>g options provided by the scanf function. As<br />

with the pr<strong>in</strong>tf function, a good understand<strong>in</strong>g of these various formats can be<br />

obta<strong>in</strong>ed only by try<strong>in</strong>g them <strong>in</strong> actual program examples.<br />

Input and Output Operations with Files<br />

So far, when a call was made to the scanf function by one of the programs <strong>in</strong> this book,<br />

the data that was requested by the call was always read <strong>in</strong> from your term<strong>in</strong>al. Similarly,<br />

all calls to the pr<strong>in</strong>tf function resulted <strong>in</strong> the display of the desired <strong>in</strong>formation <strong>in</strong> your<br />

term<strong>in</strong>al w<strong>in</strong>dow. In this section, you learn how you can read and write data from and<br />

to a file <strong>in</strong>stead.<br />

Redirect<strong>in</strong>g I/O to a File<br />

Both read and write file operations can be easily performed under many operat<strong>in</strong>g<br />

systems, such as Unix and W<strong>in</strong>dows, without anyth<strong>in</strong>g special be<strong>in</strong>g done at all to the<br />

program. If you want to write all your program results <strong>in</strong>to a file called data,for example,<br />

all that you need to do under Unix or W<strong>in</strong>dows if runn<strong>in</strong>g <strong>in</strong> a term<strong>in</strong>al w<strong>in</strong>dow is<br />

to redirect the output from the program <strong>in</strong>to the file data by execut<strong>in</strong>g the program<br />

with the follow<strong>in</strong>g command:<br />

prog > data<br />

This command <strong>in</strong>structs the system to execute the program prog but to redirect the output<br />

normally written to the term<strong>in</strong>al <strong>in</strong>to a file called data <strong>in</strong>stead. So, any values

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

Saved successfully!

Ooh no, something went wrong!