Hello,
I have a text file to read in. It is of the format
! this is a comment
! this is another comment
8 3.3 2.2 5.6 6.7
4.4 5.5 4.4
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
1 3.3 2.3 34.5 5.5 10.3e10
...
ie there are some comment lines (preceded by a space then and exclaimation mark), followed by 8 numbers (int or float) split randomly between lines, then a matrix of values in 6 columns rows (and the first int gives the number of rows - ie 8 in this case)
I have added an example txt file as an attachment in case that helps
I would like to skip the comments and blank lines, read in the 8 header numbers, then read the matrix (better I would like to read each column of the matrix into an array).
I have had a play using fscanf, getline and then stringstream to split up the lines etc, but keep getting it wrong. Any guidance would be great. Here is the best shot I have so far following the fscanf route (the getline+stringstream option was getting cumbersome)...
double NUV;
double U0;
double V0;
double ZEROSP;
double BGS1;
double BGS2;
double INTCPT;
double SLOPE;
string vis2_filename("example.tex");
ifstream vis2_file(vis2_filename.c_str());
if (vis2_file.is_open())
{
//skip the comments
while (getline(inFile,product))
{
if (product[1] = '!')
cout << product << '\n';
}
//read in the header
fscanf(vis2_file, "%lf", &NUV);
fscanf(vis2_file, "%lf", &U0);
fscanf(vis2_file, "%lf", &V0);
fscanf(vis2_file, "%lf", &ZEROSP);
fscanf(vis2_file, "%lf", &BGS1);
fscanf(vis2_file, "%lf", &BGS2);
fscanf(vis2_file, "%lf", &INTCPT);
fscanf(vis2_file, "%lf", &SLOPE);
//read in the matrix
int Nvis2 = int(NUV);
Row<double> vis2id(Nvis2);
Row<double> ucoord(Nvis2);
Row<double> vcoord(Nvis2);
Row<double> r(Nvis2);
Matrix<double> vis2_data(1,Nvis2);
Matrix<double> vis2_err(1,Nvis2);
for(int i = 0; i < Nvis2; i++)
{
fscanf(vis2_file, "%lf", &vis2id[i]);
fscanf(vis2_file, "%lf", &ucoord[i]);
fscanf(vis2_file, "%lf", &vcoord[i]);
fscanf(vis2_file, "%lf", &r[i]);
fscanf(vis2_file, "%lf", &vis2_data[0][i]);
fscanf(vis2_file, "%lf", &vis2_err[0][i]);
}
vis2_file.close();
}
Thanks in advance - I am sure this is strait forward, but file input has always been my weakness in c++ and I am going round in circles on google.
Regards
Jimbo