[Solved] C++ get file size via ifstream and set mode as ios::ate

void getSize21()
{
    ifstream wFile;
    wFile.open("log3.txt",ios::ate);
    if(!wFile.is_open())
    {
        cout<<"Open log3.txt failed!"<<endl;
    }
    streampos size;    
    size=wFile.tellg();
    cout<<"Size="<<size<<endl;
    cout<<"Finished in getSize21() and now is "<<getTimeNow()<<endl;
}

The key located at set ios::ate,ate stands for at end.
Then when size=wFile.tellg();

void getFileSize19()
{
    streampos begin,end;
    ifstream rFile("log3.txt",ios::in);
    if(!rFile.is_open())
    {
        cout<<"Open log3.txt failed!"<<endl;
    }

    begin=rFile.tellg();
    cout<<"Begin is "<<begin<<endl;
    rFile.seekg(0,ios::end);
    end=rFile.tellg();
    cout<<"End is "<<end<<endl;
    rFile.close();
    cout<<"Size is "<<(end-begin)<<" bytes"<<endl;
}

Similar Posts: