流>>读取最后一行两次
stream >> reading last line twice
我正在尝试解析/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()
。我通常喜欢小一点的。在这种情况下,这可能没有任何区别(输入错误的可能性很小),但如果你只是想跳过一行,最好将其限制在一行至少一半合理的数量。如果你只是想告诉用户有问题,那么没有理由浪费他们的时间等待你读取千兆字节的垃圾
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 从C++dll访问C#中的一行主要参数
- 在C++中,我如何接受不同于同一行的用户输入
- 如何从一行中读取特定数量的字符?(C++)
- EASTL矢量<向量<int>>连续的
- 读取最后一行代码算法 - c++ 时出现问题
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 有没有办法在一行中填充矢量图
- 需要将一行代码从C++ Qt翻译成PyQt
- 为什么如果我添加这一行,我的程序会不断询问值
- 如何使代码打印文本文件中的第一行?
- 如何在C++中以这种方式返回一行文本?
- 如何使用运算符在同一行中多次调用函数
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 而(!inputfile.eof())只读取第一行?
- 这段代码的最后一行在做什么?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- C++找出覆盖同一行的数组