I have a file that contains 4 columns and wish to take columns 2,3 and 4 for calculation.
N -9.674570 2.215540 -4.211275
HT1 -10.104954 2.712259 -4.987126
I have written a working program for this as follows.
.........
while(!infile.eof())
{
infile >> atom >> x >> y >> z;
{
if(infile.eof())
{
cout << "Finished reading file" << endl;
break;
}
xval.push_back(x);
yval.push_back(y);
zval.push_back(z);
}
}
Now, I wish to store col2, 3 and 4 as a 3D vector and tried to rewrite it as below.
..................
vector <double> xyz;
vector <vector <double> > coord;
infile.open("test1.pdb");
while(!infile.eof())
{
infile >> atom >> x >> y >> z;
{
if(infile.eof())
{
cout << "Finished reading file" << endl;
break;
}
xyz.push_back(x);
xyz.push_back(y);
xyz.push_back(z);
coord.push_back(xyz);
}
}
but I observed the following problem.
The values of x,y and z are stored one after the other yielding thrice the actual number of lines in the file.
Say, number of lines in the file is 100. The second program yielded xyz.size() as 300.
Could somebody let me know how to go about this issue?
I wanted to use coord vector for vector addition and subtraction for further calculations.
But I would wish to retain the first column too.. I modified the program using the same old theory and it worked well. Thanks.
Here is my sample code:
ifstream infile;
vector <double> xyz;
vector <vector <double> > coord;
while(!infile.eof())
{
infile >> atom >> x >> y >> z;
xyz.push_back(x);
xyz.push_back(y);
xyz.push_back(z);
coord.pus_back(xyz);
}
The mistake I did was I checked for xyz.size() which is 1-D array while coord.size() which is 2-D array gave the correct size equal to number of lines read.
One piece of advice first. Even if you discard everything else I say.
Please don't use use eof() as the loop condition. (it is not a reliable approach).
Instead, do something like this: