C++ IO流:檔案流seekp()/seekg()、tellp()/tellg()
阿新 • • 發佈:2019-01-22
一、程式碼
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; }
二、輸出結果