Skip to content
Advertisement

Invalid conversion from ‘FILE* {aka _IO_FILE*}’ to ‘int’

When trying to compile this little example…

#include <iostream>
#include <stdio.h>
#include <unistd.h>

int main(void) {
    FILE *foo;
    foo = fopen("bar.txt", "rt");
    lseek(foo, 5, SEEK_CUR);           // This line is getting compiler error
    fclose(foo);
    return 0;
}

…I get a compiler error regarding the call to lseek(). The output is:

main.cpp|8|error:     invalid conversion from ‘FILE* {aka _IO_FILE*}’ to ‘int’ [-fpermissive]|
unistd.h|334|error:   initializing argument 1 of ‘__off_t lseek(int, __off_t, int)’ [-fpermissive]|

For the record: I have also tried using both lseek(*foo, 5, SEEK_CUR); and lseek(&foo, 5, SEEK_CUR);, but that only makes things worse. (I really didn’t expect that to solve anything either.)

Referring to the man-page for lseek(3):

Synopsis
off_t lseek(int fildes, off_t offset, int whence);

Description
The lseek() function shall set the file offset for the open file description associated with the file descriptor fildes, as follows:

[……..]

I interpret this to mean that the first argument should be the file descriptor, which in this case is foo.

Q: What’s wrong here?

Advertisement

Answer

If you want to seek within file, use fseek. I never saw lseek used this way

More info: what’s the difference between `fseek`, `lseek`, `seekg`, `seekp`?

Advertisement