Get size of a file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Get size of a file

  1. #1
    Join Date
    Feb 2009
    Posts
    26

    Exclamation Get size of a file

    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.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Get size of a file

    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.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Get size of a file

    Use OS specific function.
    Thanks for your help.

  4. #4
    Join Date
    Feb 2009
    Posts
    26

    Re: Get size of a file

    Quote Originally Posted by ninja9578 View Post
    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.
    Well, I already know the full size of the file. I'm using an ftp library for downloading, but I want to know haw much that have gotten downloaded to create a progress bar.

    @Peter_APIIT
    What do you mean by OS specific function? Can you give me an example?

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Get size of a file

    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.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Get size of a file

    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.

  7. #7
    Join Date
    Feb 2009
    Posts
    26

    Re: Get size of a file

    Hey, I forgot to say that I managed to figure it out.
    I just used the stat library.

    Here's the code:
    Code:
    const char* filename=fname;
    struct stat st_buf;
    stat(filename, &st_buf);
    return st_buf.st_size;
    Thanks to all anyway!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center