INFO: Switching from Reading to Writing Files Can Garble DataLast reviewed: September 2, 1997Article ID: Q43072 |
The information in this article applies to:
SUMMARYWhen switching from reading to writing data files, it is necessary to make a call to an fsetpos(), fseek(), or rewind() function. If a call to one of these functions is not made, the file pointer may not be updated and the data could be corrupted. It is also necessary to make a call to one of these functions when switching from writing to reading. This is documented on page 275 of the "Microsoft C Optimizing Compiler Run-Time Library Reference" manual, which shipped with version 5.1 of the Microsoft C Compiler.
MORE INFORMATIONThe following program attempts to read in the first character of a file and to write it out as the second character:
Sample Code #1
#include <stdio.h> void main(void) { FILE *fp; char a; if (( fp = fopen("text.fil","r+")) != NULL) { fscanf(fp,"%c",&a); /* Read one character */ fprintf(fp,"%c",a); /* Write to the next location */ fclose(fp); } }The above program fails because there is no fseek, fsetpos, or rewind between the fscanf and fprintf to change the pointer position. The following program performs the desired operation:
Sample Code #2
#include <stdio.h> void main(void) { FILE *fp; char a; fpos_t loc; /* Storage for the current location */ if (( fp = fopen("text.fil","r+")) != NULL) { fscanf(fp,"%c",&a); /* Read one character */ fgetpos(fp,&loc); /* Get current file pointer pos */ fsetpos(fp,&loc); /* Set current file pointer pos */ fprintf(fp,"%c",a); /* Write to next location */ fclose(fp); } } Keywords : CRTIss kbfasttip Version : MS-DOS:6.00ax,7.0; OS/2:6.0,6.00a; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,2.1,2.2,4.0,4.1,5.0 Platform : MS-DOS NT OS/2 WINDOWS Issue type : kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |