C++ IO流:文件流seekp()/seekg()、tellp()/tellg()

来源:互联网 发布:js读取xml文件 编辑:程序博客网 时间:2024/05/29 10:11

一、代码

        fstream

        seekp()、seekg()

        tellp()、tellg()

1.1 文件template.c

        需要事先准备好的文件内容:


        wc  -c  template.c


1.2 代码

#include <iostream>#include <fstream>#include <cassert>using namespace std;//fstream//seekp()、seekg()//tellp()、tellg()int main(int argc, char*argv[]){        //        ifstream ifs("template.c");        assert(ifs);        ifs.seekg(0, ios::end);        streampos n = ifs.tellg();        cout<<"file len: "<<n<<endl;        ifs.close();        //        fstream fs;        fs.open("template.c", ios::in);        assert(fs.is_open());        //fs.seekp(0, ios::end);        //n = fs.tellp();        fs.seekg(0, ios::end);        n = fs.tellg();        cout<<"file len: "<<n<<endl;        fs.close();        return 0;}

二、输出结果


0 0