Determine the size of a binary file

15,318

Neither of your examples checks for failure. This program, using your first method, works perfectly well for me. It correctly identifies the size of /etc/passwd and the non-existence of /etc/motd.

#include <fstream>
#include <iostream>
#include <string>

void printSize(const std::string& address) {
  std::fstream motd(address.c_str(), std::ios::binary|std::ios::in|std::ios::ate);
  if(motd) {
    std::fstream::pos_type size = motd.tellg();
    std::cout << address << " " << size << "\n";
  } else {
    perror(address.c_str());
  }
}

int main () {
    printSize("/etc/motd");
    printSize("/etc/passwd");
}
Share:
15,318

Related videos on Youtube

Ash
Author by

Ash

Robotics researcher.

Updated on June 04, 2022

Comments

  • Ash
    Ash almost 2 years

    I'm trying to read a binary file and I need to determine its size, but regardless of the method I've tried, I'm getting a size of zero.

    For example:

    fstream cbf(address, ios::binary | ios::in | ios::ate);
    fstream::pos_type size = cbf.tellg();                   // Returns 0.
    
    char* chunk = new char[size];
    cbf.read(chunk, size);
    //...
    

    If I were to use the following:

    #include <sys/stat.h>
    struct stat st;
    stat(address.c_str(),&st);
    int size = st.st_size;
    

    The size is still zero. I've also tried the following, but it's still zero.

    File* fp;
    fp = open(address.c_str(), "rb");
    

    How do I get the size of the file?

    Thanks for the responses... I've identified the problem: The binary file I was trying to access was created during the execution, and I just had forgotten to close it before trying to read from it...

    • matth
      matth over 11 years
      Welcome to SO! Remember to check the return value from system calls. You don't check to see if stat or fstream::open succeed. You should not rely upon their results until and unless you know that they succeeded.
    • Ash
      Ash over 11 years
      i do that in my code...i check for fail() , good() , is_open() etc...i just didn't want to bother you with unnecessary code...sorry
    • matth
      matth over 11 years
      It is better to bother us with complete code then with sorta-kinda code. We can't know what your program does if you don't tell us. Please reduce your original program to the smallest possible program that still demonstrates the error and paste that complete, minimal program into your question. See sscce.org for more details.
    • Ash
      Ash over 11 years
      wow you edited my whole post...didn't realise my english was so bad!
    • danial weaber
      danial weaber over 11 years
      use ifstream cbf to open the file. else look my answer below.
  • matth
    matth over 11 years
    @Ash, what output do you get when you run my program with no changes?
  • Michael Burr
    Michael Burr over 11 years
    @Ash - are you passing an absolute path? Are you running inside an IDE where the cwd might not be what you think it is? Can you post the output of ls -al [filename] where [filename] is the exact string you pass to printSize()?
  • Ash
    Ash over 11 years
    @Michael Burr: that's what i get -rw-rw-r-- 1 ash ash 6 nov. 30 16:44 compressed_string
  • Ash
    Ash over 11 years
    @Rob: with no change : 113 1768 and for my own file 6 well that's exactly the size of my file so thanks i guess the problem comes fom the changes i make^^
  • Ash
    Ash over 11 years
    @Rob : but still when i use your function(without change) in my own code it returns 0 instead of 6 !!
  • matth
    matth over 11 years
    Can you expand upon why you think using ifstream instead of fstream would make a difference?
  • Ash
    Ash over 11 years
    thanks but it's just like all the other suggestions,it works perfectly fine when it's isolated , once i use it in my class functions it starts returning 0 instead of the real size of the file ...
  • danial weaber
    danial weaber over 11 years
    @Robᵩ :) I dont know that man, Im not a expert. this is just how I was taught and this work fine. sometimes this will work grate for ash.
  • Michael Burr
    Michael Burr over 11 years
    @Ash: are you sure about the cwd when you're running the program? Is there another compressed_string file that has zero size that the program is really dealing with?