繁体   English   中英

C ++逐行读取文件,然后使用定界符分割每一行

[英]C++ Read file line by line then split each line using the delimiter

我进行了搜索,并从以前的帖子中很好地了解了解决方案,但我仍然有些困难。

我的问题不是“随机字符串TAB号TAB号NL”

我的数据是“数字(空格),句子(空格)

我已经编辑了以下代码,但仍然无法使其工作100%,因为getline接受的参数是(流,字符串,定界符)。

由于某种原因,它也只获得句子的第一个单词,而不得到其余的单词。

上一篇


我想逐行读取一个txt文件,并且在阅读完每一行之后,我想根据选项卡“ \\ t”拆分行,并将每个部分添加到结构中的元素中。

我的结构是1 * char和2 * int

struct myStruct
{
    char chr;
    int v1;
    int v2;
}

其中chr可以包含多个字符。

一行应该是这样的:

随机字符串TAB号TAB号NL

std::ifstream file("plop");
std::string   line;

while(std::getline(file, line))
{
    std::stringstream   linestream(line);
    std::string         data;
    int                 val1;
    int                 val2;

    // If you have truly tab delimited data use getline() with third parameter.
    // If your data is just white space separated data
    // then the operator >> will do (it reads a space separated word into a string).
    std::getline(linestream, data, '\t');  // read up-to the first tab (discard tab).

    // Read the integers using the operator >>
    linestream >> val1 >> val2;
}

在下面的代码行中,数据变量将保留整行。 而且线流将被消耗,因此进一步的读数将不会产生任何结果。

std::getline(linestream, data, '\t');  // read up-to the first tab (discard tab).

相反,您可以像这样在生产线上工作

    while (std::getline(file, line))
    {
        int token1 = std::stoi(line.substr(0, line.find(" : ")));
        line.erase(0, line.find(" : ") + 3);

        int token2 = std::stoi(line.substr(0, line.find(" : ")));
        line.erase(0, line.find(" : ") + 3);

        std::string token3 = line;
    }

你到底是什么问题?

//Title of this code
//clang 3.4

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <vector>

struct Data
{
    int n1;
    int n2;
    std::string sequence;
};

std::ostream& operator<<(std::ostream& ostr, const Data& data)
{
    ostr << "{" << data.n1 << "," << data.n2 << ",\"" << data.sequence << "\"}";
    return ostr;
}

std::string& ltrim(std::string& s, const char* t = " \t")
{
    s.erase(0, s.find_first_not_of(t));
    return s;
}

std::string& rtrim(std::string& s, const char* t = " \t")
{
    s.erase(s.find_last_not_of(t) + 1);
    return s;
}

std::string& trim(std::string& s, const char* t = " \t")
{
    return ltrim(rtrim(s, t), t);
}

int main()
{
    std::string file_content{
        "1\t1\t\n"
        "2\t2\tsecond sequence\t\n"
        "3\t3\tthird sequence\n"};

    std::istringstream file_stream{file_content};
    std::string line;
    std::vector<Data> content;
    while(std::getline(file_stream, line))
    {
        std::istringstream line_stream{line};
        Data data{};
        if(!(line_stream >> data.n1 >> data.n2))
        {
            std::cout << "Failed to parse line (numbers):\n" << line << "\n";
            break;
        }

        auto numbers_end = line_stream.tellg();
        if(numbers_end == -1)
        {
            std::cout << "Failed to parse line (sequence):\n" << line << "\n";
            break;
        }

        data.sequence = line.substr(numbers_end);
        trim(data.sequence);

        content.push_back(std::move(data));
    }

    std::copy(content.cbegin(), content.cend(),
        std::ostream_iterator<Data>(std::cout, "\n"));
}

生活

与结肠同住

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM