INFO: Switching from Reading to Writing Files Can Garble Data (43072)



The information in this article applies to:

  • The C Run-Time (CRT), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Editions 5.0
    • Microsoft Visual C++, 32-bit Editions 6.0

This article was previously published under Q43072

SUMMARY

When 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. The 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);
  }
}

Modification Type:MajorLast Reviewed:12/12/2003
Keywords:kbCRT kbinfo KB43072 kbAudDeveloper