I have a data file with an unknown amount of unformatted, not needed data at the start and end of the file. But, in the middle, the data is precisely formatted and the first column will always start with one of a couple keywords. I want to skip to this part and read in that data, assigning each column to a variable. This would be simple if there wasn't the start and end "garbage" text.
Here is simple example problem. In my real code, each variable is part of a structure. I do not think this will matter, but mention it just in case...
here is my text file, I want all lines that start with keyword, and I want all columns assigned to variables
REMARK: this should be simpler REMARK: yes, it should REMARK: it is simple, you just don't see it yet Comment that doesn't start with REMARK keyword aaa 1 bbb 1 1.2555 O keyword aaa 1 bbb 2 2.2555 H keyword aaa 1 bbb 3 3.2555 C keyword aaa 1 bbb 4 4.2555 C END Arbitrary garbage texts if there were no random comments, I could use
int main{ string filename = "textfile.pdb"; string name1,name2,name3; int int1, int2; double number; ifstream inFile; inFile.open(filename.c_str()); while (inFile.good()) { inFile >> keyword >> name1 >> int1>>name2>>int2>>number>>name3; } inFile.close(); } I tried getting around this by using
while (getline(inFile,line)) This method lets me look at the line, and check if it has "keyword" in it. but then I couldn't use the convenient formatted input of the first method. I need to parse the string, which seems tricky in c++.I tried using sscanf but it complained about str to char.
The first method is nicer, I just don't know how to implement a check to only read in the line to the variables, if the line is a formatted one.