CTRL+Z Causes C 5.x and QC 2.00/2.01 fseek() to Be Off by 1

ID Number: Q41440

5.00 5.10 | 5.10

MS-DOS | OS/2

buglist5.00 buglist5.10 fixlist6.00

Summary:

The sample program below demonstrates a problem with the fseek()

function in C versions 5.00 and 5.10 and QuickC versions 2.00 and

2.01. When a file has a CTRL+Z character at the end and is opened with

fopen() in the text mode, fseek() sets the file position pointer off

by 1. If the file is open in the binary mode, fseek() works correctly.

Microsoft has confirmed this to be a problem in C version 5.00 and

5.10 and QuickC version 2.00 and 2.01 (buglist2.00 and buglist2.01).

This problem was corrected in C version 6.00 and QuickC version 2.50

(fixlist2.50).

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

FILE *df ;

long pos, pos1 ;

char buf[40] ;

void main(int argc, char *argv[])

{

if ( (df = fopen(argv[1], "rt")) == NULL )

{

sprintf(buf, "can not open %s : ", argv[1]) ;

perror (buf) ;

return ;

}

pos = ftell (df) ;

if ( fseek(df, pos, SEEK_SET) )

{

perror("fseek failed : ") ;

return ;

}

pos = ftell(df) ;

fclose(df) ;

}