如何从文件中读取数字序列?

时间:2017-06-18 13:01:49

标签: c++ file vector file-io fstream

我一直在尝试从文件中读取数字序列到2D矢量,但没有取得太大成功。

g++ -std=c++11 -c main.cpp -Iglad/include && \ g++ main.o glad.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor

中的数据
sequences.txt

我遇到了一些问题,读取了未知长度(格式)的数字序列。 例如,如果它被修复为3数字格式,输入将如下所示:

1,2,3,4,5
6,3,1
7,4,10,4,1,9

但是每个序列的数量都是未知的......

另一个问题是input_stream >> integer >> char >> integer >> char >> integer char,它不是位于每个序列的末尾,这意味着我会以某种方式通过该行直接到序列中的最后一个数字,然后将其拾取在我完成这条线之后,这可能吗?

我的代码尝试:

','

我最近开始学习文件,我阅读了大部分文献,现在正在尝试练习,所以关于如何解决这个问题的任何提示或任何有关使用文件的提示一般,我都将非常感激:)< / p>

由于

P.S。 我从这段代码得到的输出(这是错误的):#include <fstream> #include <iostream> #include <vector> void PrintMatrix(std::vector<std::vector<int>> matrix) { for(auto v : matrix) { for(auto x : v) { std::cout << x << " "; } std::cout << std::endl; } } int main() { std::fstream input_stream("sequences.txt", std::ios::in); if(!input_stream) std::cout << "Problem with opening a file"; else { std::vector<std::vector<int>> matrix; while(input_stream.good()) { std::vector<int> v; int number; char chr; while(input_stream >> number >> chr) { v.push_back(number); } matrix.push_back(v); } PrintMatrix(matrix); } return 0; }

我想得到的输出:

1 2 3 4 5

2 个答案:

答案 0 :(得分:0)

这是一个可能的解决方案,在循环内部查看整数,以确定是否应该继续下一行

#include <iostream>
#include <vector>
#include <cassert>

using std::cin;
using std::cout;
using std::endl;

int main() {
    auto data = std::vector<std::vector<int>>(1);
    auto integer = int{};
    auto current_line = 0;
    while (cin >> integer) {
        data[current_line].push_back(integer);

        // read the next character and if it is a newline add a new entry in
        // the data vector
        auto next_character = char{};
        if (cin.get(next_character)) {
            if (next_character == '\n') {
                data.emplace_back();
                ++current_line;
            }
        } else {
            break;
        }
    }

    for (const auto& vec : data) {
        for (auto integer : vec) {
            cout << integer << " ";
        }
        cout << endl;
    }

    return 0;
}

Also as noted in the comments,要记住的一件事是你应该使用istream本身检查eof并终​​止循环,而不是调用.good() .eof()等。

答案 1 :(得分:0)

这是另一种可能的解决方案,使用boost :: split方法分割每行中的数字。

#include <fstream>
#include <string>
#include <iostream>
#include <boost/algorithm/string.hpp>

void printMatrix(std::vector<std::vector<int> >& matrix)
{
    for(auto line : matrix)
    {
        for(auto num : line)
        {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }
}

int main()
{
    std::ifstream in;
    std::string line;
    in.open("sequences.txt");
    std::vector<std::vector<int> > mat;

    while(std::getline(in, line))
    {
        std::vector<std::string> text;
        std::vector<int> nums;
        boost::split(text, line, boost::is_any_of(","));
        for(auto num : text)
            nums.push_back(std::stoi(num));

        mat.push_back(nums);
    }
    printMatrix(mat);
}
相关问题