CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2000
    Location
    Malaysia
    Posts
    8

    File Size with fstream

    May I know if there is a direct way of getting the file size of a binary file (number of characters), using fstream?


  2. #2
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658

    Re: File Size with fstream

    use fstream::rdbuf()->seekoff( 0, ios_base::end )

    Rating isn't important...But gurus respect it and keep high

  3. #3
    Join Date
    May 2000
    Location
    Toronto, ON, Canada
    Posts
    3,573

    Re: File Size with fstream

    Hi,

    See this#include <iostream>
    #include <fstream>

    using namespace std;

    long GetLenFile(char *szFileName)
    {
    fstream file(szFileName, ios::in | ios::binary);

    if (!file)
    {
    TRACE("error\n");
    return -1;
    }
    else
    {
    file.seekg(0, ios_base::end);
    streampos nPos = file.tellg();
    TRACE("len file = %d\n", (long)nPos);
    return (long)nPos;
    }
    }
    // call
    long nLen = GetLenFile("readme.txt");

    Tell me if that help.

    Regards,
    Emi.
    Regards,

    Emanuel Vaduva

  4. #4
    Join Date
    Sep 2000
    Location
    Malaysia
    Posts
    8

    Re: File Size with fstream

    Sorry, it doesn't help because of the error
    "seekoff' : cannot access protected member declared in class 'std::basic_filebuf<char,struct std::char_traits<char> >'"


  5. #5
    Join Date
    Sep 2000
    Location
    Malaysia
    Posts
    8

    Re: File Size with fstream

    It works! Cheers mate.


  6. #6
    Join Date
    May 2000
    Location
    Toronto, ON, Canada
    Posts
    3,573

    Re: File Size with fstream

    You are welcome.

    Regards,
    Emi.
    Regards,

    Emanuel Vaduva

  7. #7
    Join Date
    Sep 1999
    Location
    Madrid, Spain
    Posts
    9

    Re: File Size with fstream

    Use


    m_file.seekg(0, ios_base::end);
    long nEnd = m_file.tellg();


  8. #8
    igbrus is offline Elite Member Power Poster
    Join Date
    Aug 2000
    Location
    Los Angeles
    Posts
    4,658

    Re: File Size with fstream

    Yes, other answers are more helpful

    Rating isn't important...But gurus respect it and keep high

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