PRB: _lseek() Fails on Different Offset Values

ID: Q135326

The information in this article applies to:

SYMPTOMS

The Run-Time function _lseek() fails on different offset values (number of bytes from origin). For example, in the following sample code, _lseek() fails if it seeks to the offset positions 0x1ffff, 0x2ffff, 0x3ffff and so on in a file.

CAUSE

This behavior occurs when you fail to include the required header file IO.H. This header file contains the prototype for the _lseek() function.

RESOLUTION

Include the header file.

STATUS

This behavior is by design.

MORE INFORMATION

If the code is compiled with warning level 3 or higher, the compiler will indicate that the function is not defined. It is a good idea to compile all code with a high warning level to prevent problems like this from happening.

Sample Code to Reproduce Behavior

   /* Compile options needed: None
   */ 
   #include    <stdio.h>
   #include    <stdlib.h>
   #include    <fcntl.h>
   #include    <sys\types.h>
   #include    <sys\stat.h>
   // #include    <io.h>     // Uncomment this line to fix the problem

   void main()
   {
    int  File;
    long temp;
    if ((File = _open("test.dat", O_RDWR | O_BINARY, S_IWRITE)) == -1 ){
       printf("File open error \n");
       exit(1);
    }
    temp = 0x0001fffeL;      // This read works
    if (_lseek(File, temp, SEEK_SET) == -1L)
         printf("Error reading from position:  %lx\n", temp);
    else printf("Read OK from position:        %lx\n",temp);

    temp = 0x0001ffffL;      // This read fails
    if (_lseek(File, temp, SEEK_SET) == -1L)
         printf("Error reading from position:  %lx\n", temp);
    else printf("Read OK from position:        %lx\n",temp);

    _close(File);
   }

Additional query words: 8.00 8.00c lseek
Keywords          : kb16bitonly kbLangC kbVC 
Version           : 1.0 1.5 1.51 1.52
Issue type        : kbprb

Last Reviewed: August 11, 1997