从C ++文件中读取行的首选模式是什么? [英] What's preferred pattern for reading lines from a file in C++?

查看:187
本文介绍了从C ++文件中读取行的首选模式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++教程中看到了至少两种从文件中读取行的方法:

  std :: ifstream fs (myfile.txt); 
if(fs.is_open()){
while(fs.good()){
std :: string line;
std :: getline(fs,line);
// ...

和:

  std :: ifstream fs(myfile.txt); 
std :: string line;
while(std :: getline(fs,line)){
// ...

当然,我可以添加一些检查,以确保该文件存在并打开。除了异常处理,有没有理由喜欢更冗长的第一模式?

$

  while(std :: getline(fs,line))你的标准做法是什么?

解决方案


{}

这不仅正确,而且



我假设在第一种情况下,您不是检查 fs 之后 std :: getline() as if(!fs)break; 因为如果你不这样做,那么第一种情况是完全错误的。



函数 good()应在之后使用您尝试从流中读取;它用于检查尝试是否成功。在你的第一种情况下,你不这样做。在 std :: getline()之后,假设读取成功,甚至不检查 fs.good()返回。此外,你似乎假设如果 fs.good()返回true, std :: getline 会成功读取线从流。你正好是在相反的方向:事实是,如果 std :: getline 成功地从流读取一行,然后 fs。



cplusplus的文档介绍了 href =http://en.cppreference.com/w/cpp/io/basic_ios/good =nofollow> good()


如果没有设置流的错误标志(eofbit,failbit和badbit),函数返回true。


也就是说,当您尝试从输入流读取数据时,如果尝试失败,则只会设置失败标志,并且 good()返回 false 作为失败的指示。



您希望将变量限制在循环内部,然后可以将写为 c>循环:

  for(std :: string line; std :: getline(fs,line);)
{
// use'line'
}

注意:在阅读@ john的解决方案后,但我认为它比他的版本更好。






阅读这里的详细解释为什么第二个是优选和惯用的:





或者阅读这篇写得很好的博客@Jerry Coffin: >阅读文件

I've seen at least two ways of reading lines from a file in C++ tutorials:

std::ifstream fs("myfile.txt");
if (fs.is_open()) {
  while (fs.good()) {
    std::string line;
    std::getline(fs, line);
    // ...

and:

std::ifstream fs("myfile.txt");
std::string line;
while (std::getline(fs, line)) {
  // ...

Of course, I can add a few checks to make sure that the file exists and is opened. Other than the exception handling, is there a reason to prefer the more-verbose first pattern? What's your standard practice?

解决方案

while (std::getline(fs, line))
{}

This is not only correct but preferable also because it is idiomatic.

I assume in the first case, you're not checking fs after std::getline() as if(!fs) break; or something equivalent. Because if you don't do so, then the first case is completely wrong. Or if you do that, then second one is still preferable as its more concise and clear in logic.

The function good() should be used after you made an attempt to read from the stream; its used to check if the attempt was successful. In your first case, you don't do so. After std::getline(), you assume that the read was successful, without even checking what fs.good() returns. Also, you seem to assume that if fs.good() returns true, std::getline would successfully read a line from the stream. You're going exactly in the opposite direction: the fact is that, if std::getline successfully reads a line from the stream, then fs.good() would return true.

The documentation at cplusplus says about good() that,

The function returns true if none of the stream's error flags (eofbit, failbit and badbit) are set.

That is, when you attempt to read data from an input stream, and if the attempt was failure, only then a failure flag is set and good() returns false as an indication of the failure.

If you want to limit the scope of line variable to inside the loop only, then you can write a for loop as:

for(std::string line; std::getline(fs, line); )
{
   //use 'line'
}

Note: this solution came to my mind after reading @john's solution, but I think its better than his version.


Read a detail explanation here why the second one is preferable and idiomatic:

Or read this nicely written blog by @Jerry Coffin:

这篇关于从C ++文件中读取行的首选模式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆