How can I get the size of a ASCII file?

Discussion in 'C++' started by alex, Aug 11, 2005.

  1. alex

    alex Guest

    HI,all:
    I use ofstream of stl to manange a file, but how can I get size of
    the file?

    // open file
    std::eek:fstream * log = new std::eek:fstream( "test.log" );
    // get size
    ???
     
    alex, Aug 11, 2005
    #1
    1. Advertising

  2. alex

    Stu Guest

    alex wrote:

    > HI,all:
    > I use ofstream of stl to manange a file, but how can I get size of
    > the file?
    >
    > // open file
    > std::eek:fstream * log = new std::eek:fstream( "test.log" );
    > // get size
    > ???


    Simple, you just opened it for output and truncated it, so the file size is
    zero...


    Stu
     
    Stu, Aug 11, 2005
    #2
    1. Advertising

  3. alex

    alex Guest

    I think I make a mistake.
    // Fix...
    std::eek:fstream * log = new std::eek:fstream( "test.log",ios::app|ios::eek:ut
    );
     
    alex, Aug 11, 2005
    #3
  4. alex

    hacker++ Guest

    alex wrote:
    > I think I make a mistake.
    > // Fix...
    > std::eek:fstream * log = new std::eek:fstream( "test.log",ios::app|ios::eek:ut
    > );


    Hint: It's possible to randomly select your position in the file and
    know your current positon at any time.
     
    hacker++, Aug 11, 2005
    #4
  5. alex

    Alex Vinokur Guest

    Alex Vinokur, Aug 11, 2005
    #5
  6. alex

    Guest

    #include <iostream>
    #include <fstream>
    using namespace std;

    ofstream file("f:\\test.log",ios::app|ios::eek:ut );
    file << "Hello World"; // push out some crap
    file.seekp(0,ios::end); // seek end of file
    ios::pos_type pos = file.tellp(); // get current position
    cout << pos;
    file.close();
     
    , Aug 11, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TOXiC
    Replies:
    5
    Views:
    1,283
    TOXiC
    Jan 31, 2007
  2. James O'Brien
    Replies:
    3
    Views:
    269
    Ben Morrow
    Mar 5, 2004
  3. Alextophi
    Replies:
    8
    Views:
    545
    Alan J. Flavell
    Dec 30, 2005
  4. bruce
    Replies:
    38
    Views:
    293
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    102
Loading...

Share This Page