CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    File size in bytes

    I was wondering if there is a more efficent way to calculate actual size in bytes of any file,
    here is how I am doing.
    win32 API which will do it for me. accuracy is important as I will be reading the entire file in to a shared buffer.
    Code:
    fstream reader("myfile",std:ios:binary| std::ios::in);
    unsigned int counter =0;
    unsigned char myReader;
    while(!reader.eof)
    {
       reader.read((char *)&myReader,sizeof(unsigned char));
       counter++
    }
    reader.close();
    unsigned int filesize = counter;
    I am hoping how many times the loops run will tell me the actual size in bytes. Once I know what the size is then I can allocate memory and read the entire file all at once in to the shared buffer.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: File size in bytes

    Did you try _filelength, _filelengthi64?
    Victor Nijegorodov

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

    Re: File size in bytes

    Code:
    #include <stat/sys>
    struct stat stbuf;
    stat("/tmp/file.txt", &stbuf);
    size_t size = stbuf.st_size;

  4. #4
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    Re: File size in bytes


  5. #5
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: File size in bytes

    Since you mentioned the Win32 API you might consider GetFileSize or GetFileSizeEx.
    - Guido

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: File size in bytes

    thank you all

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured