zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Socket
    剑指 Offer 14- I. 剪绳子
    剑指 Offer 29. 顺时针打印矩阵
    判断二分图
    vue生命周期以及常用标签
    滑动窗口
    二叉树
    常用算法
    动态规划
    蓄水池抽样
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15728018.html
Copyright © 2011-2022 走看看