[英]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 );
}
You can use flex and bison: 您可以使用flex和bison:
http://www.mactech.com/articles/mactech/Vol.16/16.07/UsingFlexandBison/index.html http://www.mactech.com/articles/mactech/Vol.16/16.07/UsingFlexandBison/index.html
Or Boost::regex 或Boost :: regex
Here's an example I did recently in Boost::regex 这是我最近在Boost :: regex中所做的一个示例
https://github.com/homer6/import-geoip-data https://github.com/homer6/import-geoip-data
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;
}
#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;
}
};
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.