Next: Special Characters Up: Input and Output Previous: Formatted Input Output

Whole Line Input and Output using File Pointers

Predictably, equivalents to gets and puts exist called fgets and fputs. The programmer should be careful in using them, since they are incompatible with gets and puts. gets requires the programmer to specify the maximum number of characters to be read. fgets and fputs retain the trailing newline character on the line they read or write, wheras gets and puts discard the newline.

When transferring data from files to standard input / output channels, the simplest way to avoid incompatibility with the newline is to use fgets and fputs for files and standard channels too.

For Example, read a line from the keyboard using

  fgets(data_string, 80, stdin);
and write a line to the screen using

  fputs(data_string, stdout);
Tue Jan 17 11:40:37 GMT 1995