ifstream in("readme.txt");
...
streampos pos = in.tellg(); // save current position
in.seekg(0, ios::end);
cout << "file length =" << in.tellg() << endl;
in.seekg(pos); // restore saved position
...
streampos pos = in.tellg(); // save current position
in.seekg(0, ios::end);
cout << "file length =" << in.tellg() << endl;
in.seekg(pos); // restore saved position
本文介绍了一种使用 C++ 的 ifstream 类来确定文件长度的方法。通过先保存文件的当前位置,然后将文件指针移至文件末尾并读取当前位置来获取文件长度,最后恢复文件的原始位置。
3万+

被折叠的 条评论
为什么被折叠?



