流>>读完最后一行两次

时间:2013-11-22 10:08:22

标签: c++ file-io fstream eof

我正在尝试解析/proc/partitions文件。

major minor  #blocks  name

   8        0  976762584 sda
   8        1   99998720 sda1
   8        2          1 sda2
   8        3  103561216 sda3
   8        4  291514368 sda4
   8        5    1998848 sda5

这是我机器上的/ proc / partitions文件。

#include <boost/cstdint.hpp>
#include <fstream>
#include <boost/algorithm/string/trim.hpp>
#include <boost/format.hpp>

int main(){
  std::ifstream proc_partitions_stream("/proc/partitions");
  boost::int32_t disc_partition_line_count = -2; //-1 for headers, -1 for the empty line
  //so counter is 0 when it tries to read the real entries
  while(!proc_partitions_stream.fail()){
    if(disc_partition_line_count >= 0){
      boost::uint16_t   major, minor;
      boost::uint64_t   blocks;
      std::string   label;
      proc_partitions_stream >> major >> minor >> blocks >> label;
      std::cout << boost::format("%1% %2% %3% %4%") % major % minor % blocks % label << std::endl;
      boost::algorithm::trim(label);      
    }else{
      std::string line;
      std::getline(proc_partitions_stream, line);
    }
    ++disc_partition_line_count;
  }
    return 0;
}

但它读取最后一行两次这是程序

8 0 976762584 [sda]
8 1 99998720 [sda1]
8 2 1 [sda2]
8 3 103561216 [sda3]
8 4 291514368 [sda4]
8 5 1998848 [sda5]
8 5 1998848 [] << read the last line TWICE but didn't read the label

2 个答案:

答案 0 :(得分:3)

由于您的while循环条件错误,您在> 之前测试失败,您应该在之后测试 。更好的方法是测试getline的回报。

答案 1 :(得分:2)

我会把它改写成更像的东西:

#include <boost/cstdint.hpp>
#include <fstream>
#include <boost/algorithm/string/trim.hpp>
#include <boost/format.hpp>

int main(){
  std::ifstream proc_partitions_stream("/proc/partitions");

  for (int i=0; i<2; i++)
      proc_partitions_stream.ignore(max_len, '\n');

  boost::uint16_t   major, minor;
  boost::uint64_t   blocks;
  std::string   label;
  while (proc_partitions_stream >> major >> minor >> blocks >> label) {
      std::cout << boost::format("%1% %2% %3% %4%") % major % minor % blocks % label << "\n";
      //boost::algorithm::trim(label); // was present, but non-functional?
  }
  return 0;
}

或者,定义一个小类来表示一个磁盘分区,并重载运算符&gt;&gt;和&lt;&lt;为此,以及从istream中跳过行的小函数:

class partition { 
    boost::uint16_t major, minor;
    boost uint64_t  blocks;
    std::string     label;
public:
     friend std::istream &operator>>(std::istream &is, partition &p) { 
         return is >> major >> minor >> blocks >> label;
     }

     friend std::ostream &operator<<(std::ostream &os, partition const &p) { 
         return os << boost::format("%1% %2% %3% %4%") % major % minor % blocks % label;
     }
};

std::istream &skiplines(std::istream &is, unsigned count) { 
   unsigned max_len = something; // see below
   return is.ignore(max_len, '\n');
}

然后在main中你会有类似的东西:

if (!skiplines(2)) {
    std::cerr << "Error!\n";
    return 1;
}

std::copy(std::istream_iterator<partition>(proc_partitions_stream),
          std::istream_iterator<partition>(),
          std::ostream_iterator<partition>(std::cout, "\n"));

至于max_len使用的值:很多人使用std::numeric_limits<std::streamsize>::max()。我通常喜欢一些相当小的东西。在这种情况下它可能没有任何区别(机会或格式错误的输入很小)但是如果你只是想跳过一条线,最好将它限制在一条线上至少一半合理的量。如果您只是告诉用户有问题,那么在您这样做之前没有理由浪费时间等待您阅读数十亿字节的垃圾。