I have a file, which I want to process and take only some information to modify. I want, on the same run, for the sake of speed, to write the file in another output file.
I could just pick the info I wanted (one run) and then copy the file to the output file(second run). I am just doing that in one run, so that I can avoid the second one.
Below is my code. Don't get distracted by the if conditions, these are for picking the info I want. The problem is writing the to other file.
void readPoints(char* filename, std::vector<Point>& v, char* outfilename) {
std::ifstream infile;
std::string str;
infile.open(filename);
if (!infile)
std::cout << "File not found!" << std::endl;
std::ofstream outfile;
outfile.open(outfilename);
Point::FT coords[3];
while(1) {
infile >> str;
outfile << str << "\t";
if(str == "ABET")
outfile << std::endl;
if(str == "ATOM") {
infile >> str;
outfile << str << "\t";
if(str == "16" || str == "17" || str == "18" ||
str == "20" || str == "21" || str == "22") {
for(int j = 0; j < 4; ++j) {
infile >> str;
outfile << str << "\t";
}
for (int j = 0; j < 3; ++j) {
infile >> str;
outfile << str << "\t";
coords[j] = std::stod(str);
}
Point p(3, coords);
v.push_back(p);
}
}
if(str == "END")
break;
}
infile.close();
outfile.close();
}
The problem is that infile
brings me words, not whitespaces, etc. So, I am using a tab to separate the words from each other. However, this is not enough, since the original file is not using tabs, but (white)spaces, I think.
Original file:
ATOM 1 HT1 ASP X 1 9.232 -9.194 6.798 1.00 1.00 ABET
ATOM 2 HT2 ASP X 1 8.856 -7.726 7.401 1.00 1.00 ABET
...
ATOM 50 HH11 ARG X 5 0.925 -3.001 6.677 1.00 1.00 ABET
ATOM 51 HH12 ARG X 5 0.285 -4.616 6.734 1.00 1.00 ABET
...
END
Output file:
ATOM 1 HT1 ASP X 1 9.232 -9.194 6.798 1.00 1.00 ABET
ATOM 2 HT2 ASP X 1 8.856 -7.726 7.401 1.00 1.00 ABET
...
ATOM 50 HH11 ARG X 5 0.925 -3.001 6.677 1.00 1.00 ABET
ATOM 51 HH12 ARG X 5 0.285 -4.616 6.734 1.00 1.00 ABET
...
END
Does anyone know a way to fix this? Notice that the info are the same in both files, the distance between the words is what is bothering me!