读书人

有段小代码想不通大家

发布时间: 2013-08-16 14:29:57 作者: rapoo

有段小代码想不通,求助大家

#include <string>  
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("d:\\a.txt",
fstream::ate | fstream::in | fstream::out);
if (!inOut) {
cerr << "Unable to open file!" << endl;
return EXIT_FAILURE;
}
ifstream::pos_type end_mark = inOut.tellg();
inOut.seekg(0, fstream::beg); // reposition to start of the file
int cnt = 0; // accumulator for byte count
string line; // hold each line of input
while (inOut && inOut.tellg() != end_mark
&& getline(inOut, line))
{
cnt += line.size() + 1; // add 1 to account for the newline
ifstream::pos_type mark = inOut.tellg();
inOut.seekp(0, fstream::end);// set write marker to end
inOut << cnt; // write the accumulated length
if (mark != end_mark) inOut << " ";
inOut.seekg(mark); // restore read position
}
inOut.clear(); // clear flags in case we hit an
inOut.seekp(0, fstream::end);
inOut << "\n";
inOut.close();
return 0;
}

abcd
efg
hi
j
上面这个是程序没运行前的a.txt文件,运行后变为
abcd
efg
hi
j
5 9 12 14
inOut << cnt;在这个代码前面看不太懂,当第一次getline的时候,运行到
inOut.seekp(0, fstream::end);输出流标记在j后面,因为getline输入流留下了一个换行符,运行
inOut << cnt;所以换行和5一起输出,5就跑j下面了,但是第2次循环,还是留了一个换行符,
inOut.seekp(0, fstream::end);把输出流标记标记到5后面空格的后面那个位置,这时候上面还留了个换行符,为什么没有输出来,而是直接在5后面出现了,不是5的在下面?
[解决办法]
不要使用
while (条件)
更不要使用


while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
whlie (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。


[解决办法]
你这段程序,不同编一起,执行的结果,是不同的。Dev C++ 会永无休止的写。
VC6编译不过去。
VC9,最后一行无回车
abcd
efg
hi
j5 9 12

VC9,最后一行有回车
abcd
efg
hi
j

5 9 12 14 15

读书人网 >C++

热点推荐