|
发表于 2017-8-12 10:02:04
|
显示全部楼层
在我的试验中是没有问题的:- #include <iostream>
- #include <fstream>
- using namespace std;
- int main() {
- fstream inOut("inout.txt", fstream::ate | fstream::in | fstream::out);
- size_t cnt = 0;
- string line;
- auto endMark = inOut.tellg();
- inOut.seekg(0, fstream::beg);
- while (getline(inOut, line) && inOut && inOut.tellg() != endMark) {
- cnt = line.size() + 1;
- auto tempmark = inOut.tellg();
- inOut.seekp(0, fstream::end);
- inOut << cnt;
- if (tempmark != endMark)
- inOut << " ";
- inOut.seekg(tempmark);
- }
- inOut.seekp(0, fstream::end);
- inOut << "\n";
- return 0;
- }
复制代码
其中的 inout.txt 文件内容如下:
- line1: hello
- line2: world!!!
- line3: 012314
- line4: 01231484
- line5: 012314484
复制代码
注意最后留了个空行,输出结果如下:
- line1: hello
- line2: world!!!
- line3: 012314
- line4: 01231484
- line5: 012314484
- 13 16 14 16
复制代码
当然我把 inout.txt 的换行存为 unix-style,也就是 LF 而不是 CRLF,所以第一行统计的数目为13。
你的统计结果有问题我估计是修改后没有即时保存。我用的比较简单的vscode编辑器,即时观察输出效果:
|
|