tags:

views:

155

answers:

1

Hi, I have the following code in C

long from = atol(buffer);
printf("From: %ld\n", from);

int file_fd = open(fullPath, O_RDONLY);
if (file_fd == -1) error("Error opening file");

if (from > -1)
{
    int a = lseek(file_fd, from, SEEK_SET);
    if (a == -1) 
        error("Error in lseek");
}

The lseek operation is returning Error in lseek: Invalid argument, as a result of

void error(char *msg)
{
    perror(msg);
    exit(1);
}

Do you have any idea how can I debug it so I can find out what's wrong? I thought it was very trivial but it's driving me crazy.

Thanks

+2  A: 

Have you tried strace on the process? I'd check that before digging into the source to find out where is "invalid argument" thrown.

Seeing Sean's answer, have you included the proper headers?

   #include <sys/types.h>
   #include <unistd.h>
Vinko Vrsalovic
I had unistd.h missing! Thanks a lot
Juan Manuel
Compile with -Wall to catch some problems like that.You should probably have #include <stdlib.h> for that atol() .
Sean A.O. Harney
In fact, why not use the C standard stream functions - fopen, fseek et al?
anon
I can't emphasise enough how confusing it can be if you miss unistd.h (it can work for 32bit off_t but not 64bit, even with _FILE_OFFSET_BITS=64 set). Always compile with -Wall to point this out!
Draemon