我有一个包含表格行的文件,
double mass, string seq, int K, int TS, int M, [variable number of ints]
688.83 AFTDSK 1 1 0 3384 2399 1200
790.00 MDSSTK 1 3 1 342 2
Run Code Online (Sandbox Code Playgroud)
我需要一种(最好是简单的)方法来解析这个文件而不需要提升.如果每行的值的数量是恒定的,那么我会在这里使用解决方案.
每一行都将成为Peptide类的对象:
class Peptide {
public:
double mass;
string sequence;
int numK;
int numPTS;
int numM;
set<int> parents;
}
Run Code Online (Sandbox Code Playgroud)
前三个整数在对象中具有特定的变量名称,而所有以下整数都需要插入到集合中.
我很幸运能得到两个非常棒的响应,但运行时差异使C实现成为我的最佳答案.
Jon*_*rdy 10
如果要使用C++,请使用C++:
std::list<Peptide> list;
std::ifstream file("filename.ext");
while (std::getline(file, line)) {
// Ignore empty lines.
if (line.empty()) continue;
// Stringstreams are your friends!
std::istringstream row(line);
// Read ordinary data members.
Peptide peptide;
row >> peptide.mass
>> peptide.sequence
>> peptide.numK
>> peptide.numPTS
>> peptide.numM;
// Read numbers until reading fails.
int parent;
while (row >> parent)
peptide.parents.insert(parent);
// Do whatever you like with each peptide.
list.push_back(peptide);
}
Run Code Online (Sandbox Code Playgroud)