PRB: _lseek() Fails on Different Offset Values

Last reviewed: August 8, 1997
Article ID: Q135326
The information in this article applies to:
  • The C Run-time (CRT) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52

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 : CLngIss kb16bitonly
Version : 1.0 1.5 1.51 1.52
Issue type : kbprb


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.