Hey.
I need a way to return size of a file without opening it since it is already open from another program (actually it gets downloaded so it isn't even completed yet).
Is there a way to do this!?
Thanks.
Printable View
Hey.
I need a way to return size of a file without opening it since it is already open from another program (actually it gets downloaded so it isn't even completed yet).
Is there a way to do this!?
Thanks.
Well, if it's not local, then only the server can tell you how big it is. Where is the file coming from, is it coming from a server in which you know the actions? I'm sure most file servers have protocol for that.
Use OS specific function.
There is no standard C++ way, so you have to use the API that's provided by the operating system you want the program to run on.
Check fstat for UNIX-like OSs, GetFileSize(Ex) for MS Windows.
Note however that this might not work. If the file is open and written to, the operating system may report the filesize as 0. You should better check the ftp library you are using if it has a way to tell how many bytes have been transferred so far. I'm sure you will find something there.
Oh, well what are you using to download it? If you're using libcurl I can help you, if not, then sorry.
libCURL has a callback which you have to write, which is used to write the data. You can use that to found how many bytes have been written so far, and if you already know how big it is, creating a progess bar and updating it is easy.
Hey, I forgot to say that I managed to figure it out.
I just used the stat library.
Here's the code:
Thanks to all anyway!Code:const char* filename=fname;
struct stat st_buf;
stat(filename, &st_buf);
return st_buf.st_size;