【发布时间】:2009-09-29 19:01:59
【问题描述】:
我有一个 n X 2 矩阵按原样存储在一个文本文件中。我尝试用 C++ 阅读它
nb_try=0;
fin>>c_tmp>>gamma_tmp;
while (!fin.eof( )) //if not at end of file, continue reading numbers
{
// store
cs_bit.push_back(c_tmp);
gammas_bit.push_back(gamma_tmp);
nb_try++;
// read
fin>>c_tmp;
assert(!fin.fail( )); // fail at the nb_try=n
if(fin.eof( ))break;
fin>>gamma_tmp; // get first number from the file (priming the input statement)
assert(!fin.fail( ));
}
当 nb_try==n 时,第一个断言失败,即 fin.fail( ) 为真,这发生在它尝试读取第一个不存在的数字时。但是为什么 fin.eof( ) 在读完最后一个数字后不是真的呢?这是否意味着只有在读取第一个不存在的数字时它才成为真的? fin.fail( ) 和 fin.eof( ) 是否同时变为 true 是真的吗?
感谢和问候!
【问题讨论】: