Making Header Files More Efficient

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

Integer Input and Output
For certain types of applications, it may be necessary to read and write stream (or buffered) integer information. The C language incorporates two functions for this purpose: getw( ) and putw( ).
Using getw( ) and putw( )
The complementary functions getw( ) and putw( ) are very similar to the functions getc( ) and putc( ) except that they input and output integer data instead of character data to a file. You should use both getw( ) and putw( ) only on files that are opened in binary mode. The following program opens a binary file, writes ten integers to it, closes the file, and then reopens the file for input and echo print:
/*
*   badfil.c
*   A C program that uses the functions getw and putw on
*   a file created in binary mode.
*   Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#include <stdlib.h>

#define ISIZE 10

void main( )
{
 FILE
*pfi;
 int ivalue,ivalues[ISIZE],i;

 pfi = fopen(“a:\\integer.dat”, “wb”);
 if(pfi == NULL) {
   printf(“File could not be opened”);
   exit(1);
 }
 for(i = 0; i < ISIZE; i++) {
   ivalues[i]=i+1;
   putw(ivalues[i],pfi);
 }

 fclose(pfi);

 pfi=fopen(“a:\\integer.dat”, “r+b”);
 if(pfi == NULL) {
   printf(“File could not be reopened”);
   exit(1);
 }

   while(!feof(pfi)) {
     ivalue = getw(pfi);
     printf(“%3d”,ivalue);
   }
}
Look at the output from this program and see if you can figure out what went wrong:
1  2  3  4  5  6  7  8  9 10 -1
Because the integer value read in by the last loop may have a value equal to EOF, the program uses the function feof( ) to check for the end-of-file marker. However, the function does not perform a look-ahead operation as do some other high-level language end-of-file functions. In C, an actual read of the end-of-file value must be performed in order to flag the condition.
To correct this situation, the program needs to be rewritten using what is called a priming read statement:
/*
*   geputw.c
*   A C program that uses the functions getw and putw on
*   a file created in binary mode.
*   Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>

#define ISIZE 10

void main( )
{
 FILE
*pfi;
 int ivalue,ivalues[ISIZE],i;
 pfi = fopen(“a:\\integer.dat”, “w+b”);
 if(pfi == NULL) {
   printf(“File could not be opened”);
   exit(1);
 }
 for(i = 0; i < ISIZE; i++) {
   ivalues[i]=i+1;
   putw(ivalues[i],pfi);
 }

 fclose(pfi);

 pfi=fopen(“a:\\integer.dat”, “rb”);
 if(pfi == NULL) {
   printf(“File could not be reopened”);
   exit(1);
 }

 ivalue = getw(pfi);
 while(!feof(pfi)) {
   printf(“%3d”,ivalue);
   ivalue=getw(pfi);
 }
}
Before the program enters the final while loop, the priming read is performed to check to see if the file is empty. If it is not, a valid integer value is stored in ivalue. If the file is empty, however, the function feof( ) will acknowledge this, preventing the while loop from executing.
Also notice that the priming read necessitated a rearrangement of the statements within the while loop. If the loop is entered, then ivalue contains a valid integer. Had the statements within the loop remained the same as the original program, an immediate second getw( ) function call would be performed. This would overwrite the first integer value. Because of the priming read, the first statement within the while loop must be an output statement. This is next followed by a call to getw( ) to get another value.
Suppose the while loop has been entered nine times. At the end of the ninth iteration, the integer numbers 1 through 8 have been echo printed and ivalue has been assigned a 9. The next iteration of the loop prints the 9 and inputs the 10. Since 10 is not EOF, the loop iterates, causing the 10 to be echo printed and EOF to be read. At this point, the while loop terminates because the function feof( ) sees the end-of-file condition.
These two simple example programs should highlight the need to take care when writing code that is based on the function feof( ). This is a peculiarly frustrating programming task since each high-level language tends to treat the end-of-file condition in a different way. Some languages read a piece of data and at the same time look ahead to see the end-of-file; others, like C, do not.

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