chakolatemilk
chakolatemilk

Reputation: 883

Finding file size with c++

More problems, I'm genuinely confused, and it's bothering me to no end, I've tried almost everything with dirent.h as well. But here's an example of what I want:

#include <iostream>
#include "DataEventResponseMsg.idl"
using namespace std;

int main(){
    cout << "This is the size: " << sizeof(DataEventResponseMsg) << endl;
    return 0;
}

What that did was include that specific file and finds the size of it and prints it. I want to do that, but I need it to open a directory filled with .idl files and print which file it is and the size of it using sizeof..

I've tried to do it with dirent.h and opening the directory, and placing the contents into ent->d_name, and then looking for the sizeof the d_name and such, but all that does is print the size of the pointer.. and I tried placing the files into an array, but doing sizeof only prints the size of the array. I want it to print the sizeof the actual file as if I was including them in the header file like the one i posted..

Is this possible at all? I'm so confused and I need to get this to work, but I need help.

Please help.

EDIT ------

So I've done this with dirent.h:

#include <iostream>
#include <dirent.h>
#include <string.h>
#include <fstream>

using namespace std;

int main( int argc, char* argv[] ){
char FileArr[20][256];
DIR *dir;
FILE *fp;
int i = 0;
int k;
struct dirent *ent;

dir = opendir (argv[1]);
if( dir != NULL ){
    while(( ent = readdir ( dir ) ) != NULL ){
        strcpy( FileArr[i], ent->d_name );
        fp = fopen( FileArr[i], "rw" );
        cout << FileArr[i] << ", " << sizeof(FileArr[i]) << endl;
        fclose(fp);
        i++;
    }
    closedir( dir );
}
return 0;
}

And this opened up the directory from the command line, and it placed the contents into ent->d_name, and I copied those strings into an array and tried to find the size of all the contents in the array. However, doing sizeof only does the size of the array, of if i used a pointer, of the pointer. And I want to find the sizeof of the actual file, not anything that's holding the file string/name.

Upvotes: 2

Views: 9308

Answers (2)

phonetagger
phonetagger

Reputation: 7883

The answer lethal-guitar provided is a nice OS-independent way of getting the file sizes, so long as you already know what files are in a directory. If you don't know what files are in a directory, then you're back to using platform-specific tools, for example, dirent for Linux, as you suggest. Below is a trivial example of how to use dirent:

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
    struct stat file_stats;
    DIR *dirp;
    struct dirent* dent;

    dirp=opendir("."); // list files in the current directory (replace with any other path if you need to)
    do {
        dent = readdir(dirp);
        if (dent)
        {
            printf("  file \"%s\"", dent->d_name);
            if (!stat(dent->d_name, &file_stats))
            {
                printf(" is size %u\n", (unsigned int)file_stats.st_size);
            }
            else
            {
                printf(" (stat() failed for this file)\n");
            }
        }
    } while (dent);
    closedir(dirp);
}

Upvotes: 2

lethal-guitar
lethal-guitar

Reputation: 4519

There are several ways to get a file's size in C++. One way is to seek to the end of the file end then ask for the file pointer's position:

ifstream file("filename.idl", ios::in);
file.seekg(0, ios::end);
auto fileSize = file.tellg();

That is even given as an example on this page:

http://www.cplusplus.com/reference/istream/istream/tellg/

Besides that, there are other (OS-specific) solutions, like pointed out in the comments, but this approach should work regardless of your platform.

Upvotes: 5

Related Questions