-2

I want to find the length of a binary file. I tried using ftell (code below), but I get length 64, however, if I look at the properties of the file it should have 30 bytes.

What am I getting wrong?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    FILE* file = fopen(argv[1], "rb");
    int st = 0;

    fseek(file, 0, SEEK_END);
    int fileLen=ftell(file);
    printf("%d", fileLen);
    fseek(file, 0, SEEK_SET);

    ...
}
Nina
  • 499
  • 6
  • 16
  • 1
    What "properties" are you looking at? – norok2 Sep 02 '22 at 11:34
  • check error values from io function calls (your code "works for me" (c)) – OznOg Sep 02 '22 at 11:36
  • 2
    Also, `ftell` actually returns a `long int`, though I don't think that's the problem here. – 500 - Internal Server Error Sep 02 '22 at 11:36
  • @norok2 I looking at the size property in Linux. – Nina Sep 02 '22 at 11:38
  • @Nina how are you looking at it? with a syscall? – norok2 Sep 02 '22 at 11:39
  • @OznOg I'm using Linux and I'm sure the file should have 30 bytes because it also says int the instructions from where I downloaded it that it should have 30 bytes, so the question is more why does ftell give a different value. – Nina Sep 02 '22 at 11:40
  • @norok2 I did a right click on the file and chose properties. – Nina Sep 02 '22 at 11:41
  • 2
    If you open a terminal and in the shell uses the command `stat /path/to/file`, what does it report for size? – Some programmer dude Sep 02 '22 at 11:43
  • 1
    @Someprogrammerdude it says Size: 30 – Nina Sep 02 '22 at 11:45
  • 2
    The you have to think: Are you really using the exact same file, with the exact same path, when you run your program? How do you run the program? From a terminal? From an IDE or similar? What is the argument you pass to the program when running? – Some programmer dude Sep 02 '22 at 11:47
  • @Someprogrammerdude I am running it from the terminal like this: ./first file01 . (file01 is the name of the file). I am sure this is the right file. If I copy it in a for loop and save it to another file the values are the same in both files. – Nina Sep 02 '22 at 11:51
  • Does this answer your question? [How do you determine the size of a file in C?](https://stackoverflow.com/questions/8236/how-do-you-determine-the-size-of-a-file-in-c) – abdo Salm Sep 02 '22 at 11:56

1 Answers1

2

First let's create a 30 bytes long file.

I'm using a shell command to create it :

echo -n -e $(printf "\x86%.0s" {1..30}) > bytefile
stat bytefile
stat --format="%s bytes" bytefile

The output is as following :

File: bytefile
  Size: 30              Blocks: 8          IO Block: 4096   regular file
[...]
30 bytes

I'll use a slighty modified version of your code (I've just removed unusued parts for the tests) :

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    FILE* file = fopen(argv[1], "rb");

    fseek(file, 0, SEEK_END);
    int fileLen=ftell(file);
    printf("%ld\n", fileLen);
    fclose(file);
}

And its output is about 30 bytes, with no compilation warnings.

It seems that your code doesn't any error, and is doing the job correcty. Your issue can comes from the way you are measuring the file size, the environment of executing the program, or just that the file you're trying with is just the wrong one.

Could you try to use a builtin method to compute the size ? Following the code from here, I've made this snippet (which give me the same result as before) :

#include <stdio.h>                                                                                         
#include <stdlib.h>                                                                                        
                                                                                                           
#include <sys/types.h>                                                                                     
#include <sys/stat.h>                                                                                      
#include <unistd.h>                                                                                        
                                                                                                           
int main(int argc, char **argv) {                                                                          
    FILE* file = fopen(argv[1], "rb");                                                                     
    int fileLen, fd;                                                                                       
    struct stat buff;                                                                                      
                                                                                                           
    fseek(file, 0, SEEK_END);                                                                              
    fileLen=ftell(file);                                                                                   
    printf("%ld\n", fileLen);                                                                               
                      
    // New method, with fstat.                                                                                 
    fd = fileno(file);                                                                                     
                                                                                                           
    fstat(fd, &buff);                                                                                      
    off_t size = buff.st_size;                                                                             
    printf("%ld\n", size);                                                                                 
                                                                                                           
    fclose(file);                                                                                          
}

If you still got issues after reviewing all of that could you please give a little more details on the workflow you have ? What type of file are you using, the environment your writing and reading in etc.

yaourtiere
  • 36
  • 6
  • It works with your file and code. I'll look at my files again. Thanks for the help. – Nina Sep 02 '22 at 12:29
  • I'm happy I could help you, if the issue persists I'm interested in further research. – yaourtiere Sep 02 '22 at 12:32
  • @Nina I'm sorry to say that it indicates that there's something missing from your question. Either the code isn't a good enough [mre] or that we don't have enough information about the file itself. Please take some time to refresh [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 02 '22 at 12:50