流>>读取最后一行两次

stream >> reading last line twice

本文关键字:gt 一行 两次 读取 最后      更新时间:2023-10-16

我正在尝试解析/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

因为while循环条件错误,所以在阅读之前测试失败,应该在阅读后测试失败。更好的方法是测试getline的返回。

我会把它重写成更像:

#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;
}

或者,定义一个小类来表示磁盘分区,并重载运算符>>和<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()。我通常喜欢小一点的。在这种情况下,这可能没有任何区别(输入错误的可能性很小),但如果你只是想跳过一行,最好将其限制在一行至少一半合理的数量。如果你只是想告诉用户有问题,那么没有理由浪费他们的时间等待你读取千兆字节的垃圾