简体   繁体   English

在C ++中解析txt文件

[英]parsing a txt file in c++

Hi I'm coming from C# and have been working with c++ alot lately and I need help, I have a text file(500mb) that i need to load into an array(matrix). 嗨,我来自C#,最近一直在使用c ++,我需要帮助,我有一个文本文件(500mb),需要将其加载到数组(矩阵)中。 the format on the text file is 文本文件上的格式为

specimen  date      result
E1       111111    0.5

How can i just extract out the result of this and put only that into my matrix where each column would represent a different specimen? 我如何才能提取出结果并将其仅放入矩阵中,其中每列将代表不同的样本? I know in C# i could eaisly parse this but i dont know how to do it in c++ thank you for your help ,i just want to know the commands or functions that I would need to know 我知道在C#中我可以轻松地对此进行解析,但是我不知道如何在c ++中进行解析,谢谢您的帮助,我只想知道我需要知道的命令或功能

                 E1 ............En
result@ time 1   .................
                 .               .
                 . . .  . . . . ..
result@time n    .................

there are 7000 specimens , and 3 years worth of dates , the results range from 0.1 to 20000 有7000个标本和3年的日期,结果范围从0.1到20000

You could use file streams from the standard library, and formatted input: 您可以使用标准库中的文件流和格式化的输入:

std::vector< double > results;

std::ifstream input( "yourfile.txt" );
std::string specimen;
int date;
double result;

while( ( input >> specimen >> date >> result ) )
{
    ... do something with result ...
    results.push_back( result );
}
typedef std::string specimen;
typedef std::string date;
typedef std::pair<specimen, date> sample;

std::map<sample, double> data;

void parse(std::istream& in) {
    sample t;
    double result
    in.ignore(1024, '\n'); //skip the title line
    while(in >> t.first>> t.second>> result)
        data[t] = result;
}

Define how to read and parse one line from your file. 定义如何读取和解析文件中的一行。

#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>

struct Data
{
    double value;
    operator double() const {return value;}
    friend std::istream& operator>>(std::istream& stream, Data& d)
    {
        static std::string  specimen; // Assume specimen has no spaces it in.
        static std::string  date;     // Treat the date as a string as it probably has '/'

        std::string  line;
        // Read and process each line independently (hopefully compensating for errors). 
        std::getline(stream, line);
        std::stringstream   linestream(line);

        // Read and ingore the first two items. Then save the value.
        linestream >> specimen >> date >> d.value;
        return stream;
    }
};

Now just read the file into the vector. 现在,只需将文件读入向量即可。

int main()
{
    std::ifstream          file("data.file");
    std::vector<double>    data;

    // Ignore the header line.
    file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Copy the data from the file into the vector.
    std::copy(std::istream_iterator<Data>(file),
              std::istream_iterator<Data>(),
              std::back_inserter(data)
             );
}

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

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