Structures

Chapter 11 - Complete I/O in C

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

String Input and Output
In many applications, it is more natural to handle input and output in larger pieces than characters. For example, a file of boat salesmen may contain one record per line, with each record consisting of four fields: salesperson’s name, base pay, commission, and number of boats sold, with white space separating the fields. It would be very tedious to use character I/O.
Using gets( ), puts( ), fgets( ), and fputs( )
Because of the organization of the file, it would be better to treat each record as a single character string and read or write it as a unit. The function fgets( ), which reads whole strings rather than single characters, is suited to this task. In addition to the function fgets( ) and its inverse fputs( ), there are also the macro counterparts gets( ) and puts( ).
The function fgets( ) expects three arguments: the address of an array in which to store the character string, the maximum number of characters to store, and a pointer to a file to read. The function will read characters into the array until the number of characters read in is one less than the size specified, all of the characters up to and including the next newline character have been read, or the end-of-file is reached, whichever comes first.
If fgets( ) reads in a newline, the newline will be stored in the array. If at least one character was read, the function will automatically append the null string terminator \0. Suppose the file SALESMAN.DAT looks like this:
Pat Pharr 32767 0.15 30
Beth Mollen 35000 0.12 23
Gary Kohut 40000 0.15 40
Assuming a maximum record length of 40 characters including the newline, the following program will read the records from the file and write them to the standard output:
/*
*   fgets.c
*   A C program that demonstrates how to read
*   in whole records using fgets and prints
*   them out to stdout using fputs.
*   Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define INULL_CHAR 1
#define IMAX_REC_SIZE 40

void main( )
{
 FILE
*pfinfile;
 char crecord[IMAX_REC_SIZE + INULL_CHAR];

 pfinfile=fopen(“a:\\salesman.dat”, “r”);
 while(fgets(crecord,IMAX_REC_SIZE +INULL_CHAR,pfinfile) != NULL)
   fputs(crecord,stdout);
 fclose(pfinfile);
}
Because the maximum record size is 40, you must reserve 41 cells in the array; the extra cell is to hold the null terminator \0. The program does not generate its own newline when it prints each record to the terminal, but relies instead on the newline read into the character array by fgets( ). The function fputs( ) writes the contents of the character array, crecord, to the file specified by the file pointer, stdout.
If your program happens to be accessing a file on a disk drive other than the one where the compiler is residing, it may be necessary to include a path in your filename. Notice this description in the preceding program; the double backslashes (\\) are necessary syntactically to indicate a subdirectory. Remember that a single \ usually signals that an escape or line continuation follows.
While the functions gets( ) and fgets( ) are very similar in usage, the functions puts( ) and fputs( ) operate differently. The function fputs( ) writes to a file and expects two arguments: the address of a null-terminated character string and a pointer to a file; fputs( ) simply copies the string to the specified file. It does not add a newline to the end of the string.
The macro puts( ), however, does not require a pointer to a file since the output automatically goes to stdout, and it automatically adds a newline character to the end of the output string. An excellent example of how these functions differ can be found in Chapter 9 in the section “String Functions and Character Arrays.”

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net