1

I am using lseek just to find the size of a file and lseek returns less bytes than the file's actual size. I think the code is right and I can't explain why that happens.

When i ran the program for the first time it worked well. Then I added some data to the file so it changed its size. Since then, when I run the program again with the new file, the result is always the size of the old file.

Any help appreciated!

int main(int argc,char* argv[]){
    int out_file_size=0;
    int fd_prognameout;

    fd_prognameout=open(argv[1],O_RDWR | O_CREAT,00700);

    if(fd_prognameout == -1){
        perror("Error:");
        return(0);
    }

    lseek(fd_prognameout,0,SEEK_SET);
    out_file_size = lseek(fd_prognameout,0,SEEK_END);
    lseek(fd_prognameout,0,SEEK_SET);

    printf("The size of out file is: %d\n",out_file_size);
    
    close(fd_prognameout);

    return(0);
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the code. If it is just the `lseek` you have issues with then you shouldn't need to show most of the code after the `lseek` calls. Provide the smallest amount of code necessary to see the problem as well as debugging info such as the listing of the file. – kaylum Jun 05 '21 at 11:44
  • 1
    Is it possible you have two files with the same name in different directories and are modifying the one your program is not opening? – Retired Ninja Jun 05 '21 at 11:49
  • @RetiredNinja Thanks for the help. I ran the program once again and the problem was gone. I don't know how. Maybe it's something like you said. But the fact is that I did no change to my code neither to the file name. If you have any idea please tell me. – Grigorios Garoufalis Jun 05 '21 at 11:57
  • @GrigoriosGaroufalis Then most likely your code contains undefined behaviour. – DarkAtom Jun 05 '21 at 12:04
  • @DarkAtom Do you see anything taht cause the undefined behaviour? If yes, I would like to know in order to change it. – Grigorios Garoufalis Jun 05 '21 at 12:19
  • 1
    *I am using lseek just to find the size of a file* It works, but it's not thread-safe and you can just use [fstat()](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html) – Andrew Henle Jun 05 '21 at 12:41

1 Answers1

1

First, lets change these lines

lseek(fd_prognameout,0,SEEK_SET);
out_file_size = lseek(fd_prognameout,0,SEEK_END);
lseek(fd_prognameout,0,SEEK_SET);

to

//get the current file position
off_t current_pos = lseek(fd_prognameout,0,SEEK_CUR);
//get the last position (size of file)
off_t out_file_size = lseek(fd_prognameout,0,SEEK_END);
//reset to the previous position
lseek(fd_prognameout,current_pos,SEEK_SET);

Now to your problem. Modifications to a file might not be immediately visible (due to caching/buffering by the kernel). After modifying a file, either run the command sync or call in your code the functions fsync() or fdatasync().

Erdal Küçük
  • 4,810
  • 1
  • 6
  • 11