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

Attachments
! Calibrated visibility file from UKRATVIS2        
 ! Contents:  NUV U0 V0 ZEROSP BGS INTCPT SLOPE from
 ! cal file then n, u, v, r, Vis and Var_vis        
 ! SRC and CAL files used were:                     
 !  10216.avvis                             
 !  cals.avvis                              

         105
    1.00       65.0    
   1.667E+13  0.000E+00  1.444E+07
    1.35     -3.636E-02
      1     6.00    49.00    16.76   1.93E-01   3.09E-06
      2     2.00    57.00     8.06   5.71E-01   1.93E-05
      3     5.00    57.00     8.94   5.02E-01   1.75E-05
      4    15.00    83.00    22.80   1.62E-01   2.33E-06
      5    10.00    99.00    35.17   2.66E-01   8.36E-06
      6    14.00    91.00    29.07   1.48E-01   2.39E-06
      7    28.00    43.00    34.83   2.40E-01   8.01E-06
      8    23.00    59.00    22.80   2.07E-01   4.76E-06
      9    27.00    51.00    29.53   2.10E-01   5.56E-06
     10    14.00    25.00    42.06   1.51E-01   2.68E-06
     11     4.00    74.00     9.49   5.30E-01   1.87E-05
     12     3.00    40.00    25.08   1.99E-01   4.07E-06
     13     3.00    82.00    17.12   1.80E-01   2.67E-06
     14    12.00    74.00    14.21   4.53E-01   1.74E-05
     15    25.00    34.00    39.20   2.02E-01   5.00E-06
     16    21.00    42.00    30.48   2.32E-01   6.57E-06
     17    16.00    58.00    16.55   2.97E-01   8.10E-06
     18    20.00    50.00    24.21   2.13E-01   4.80E-06
     19     7.00    24.00    41.44   1.66E-01   3.14E-06
     20     8.00    66.00     7.07   7.21E-01   3.27E-05
     21    18.00    33.00    36.24   1.88E-01   4.34E-06
     22    30.00    83.00    34.13   1.34E-01   2.18E-06
     23    25.00    99.00    41.62   1.47E-01   2.71E-06
     24    29.00    91.00    38.21   5.27E-02   3.16E-07
     25    16.00    65.00    15.00   3.76E-01   1.14E-05
     26     3.00   105.00    40.05   2.19E-01   5.63E-06
     27    27.00    74.00    27.51   2.04E-01   4.80E-06
     28    10.00   106.00    41.98   2.14E-01   5.16E-06
     29    38.00    50.00    39.92   2.08E-01   6.11E-06
     30    33.00    66.00    32.02   1.01E-01   1.11E-06
     31    37.00    58.00    36.67   1.38E-01   2.67E-06
     32    24.00    32.00    40.22   1.88E-01   4.40E-06
     33    11.00    72.00    12.21   5.47E-01   2.43E-05
     34    35.00    41.00    41.62   2.29E-01   6.92E-06
     35    18.00    73.00    18.79   3.49E-01   1.15E-05
     36     9.00    32.00    33.96   2.24E-01   6.06E-06
     37    17.00    42.00    28.02   2.23E-01   5.54E-06
     38    12.00    58.00    13.04   3.83E-01   1.17E-05
     39    16.00    50.00    21.21   2.06E-01   4.17E-06
     40     3.00    24.00    41.05   1.90E-01   4.19E-06
     41    12.00    66.00    11.05   5.44E-01   2.37E-05
     42    14.00    33.00    34.54   1.99E-01   4.80E-06
     43     5.00    65.00     4.00   8.64E-01   9.53E-06
     44    14.00   106.00    43.01   2.10E-01   5.11E-06
     45    22.00    73.00    22.47   2.66E-01   7.32E-06
     46    35.00    80.00    37.16   1.45E-01   2.70E-06
     47    30.00    96.00    42.45   7.09E-02   5.77E-07
     48    34.00    88.00    40.22   8.59E-02   9.72E-07
     49    21.00    62.00    20.22   2.40E-01   6.04E-06
     50     8.00   102.00    37.66   2.64E-01   8.34E-06
     51    32.00    71.00    31.58   1.37E-01   2.39E-06
     52    15.00   103.00    40.50   2.38E-01   6.83E-06
     53     6.00    62.00     5.83   6.73E-01   1.82E-05
     54     4.00    35.00    30.15   2.43E-01   7.02E-06
     55    19.00   103.00    42.05   2.15E-01   5.92E-06
     56    42.00    55.00    42.20   1.63E-01   3.61E-06
     57    37.00    71.00    36.50   1.09E-01   1.57E-06
     58    41.00    63.00    40.05   8.13E-02   8.34E-07
     59    28.00    37.00    38.90   2.27E-01   6.96E-06
     60    15.00    77.00    18.44   3.23E-01   9.24E-06
     61    39.00    46.00    42.49   2.16E-01   5.94E-06
     62    22.00    78.00    24.70   2.38E-01   5.87E-06
     63    13.00    37.00    30.46   2.12E-01   5.22E-06
     64     5.00    70.00     6.40   7.50E-01   1.97E-05
     65    26.00    78.00    28.18   2.08E-01   5.04E-06
     66     8.00    40.00    25.96   2.01E-01   4.04E-06
     67    10.00    80.00    17.49   2.58E-01   5.58E-06
     68     5.00    96.00    31.26   2.80E-01   8.60E-06
     69     9.00    88.00    24.35   1.30E-01   1.59E-06
     70     6.00    68.00     5.83   8.09E-01   2.25E-05
     71    19.00    28.00    41.15   1.64E-01   3.51E-06
     72     7.00    71.00     8.49   6.45E-01   2.63E-05
     73    12.00    27.00    39.56   1.68E-01   3.45E-06
     74    21.00    68.00    20.22   2.63E-01   6.84E-06
     75    29.00    35.00    41.04   2.14E-01   6.11E-06
     76     8.00    27.00    38.64   1.96E-01   4.82E-06
     77    26.00    65.00    25.00   1.78E-01   3.54E-06
     78    33.00    40.00    40.61   2.30E-01   7.16E-06
     79    40.00    74.00    40.02   1.04E-01   1.36E-06
     80    35.00    90.00    42.20   6.73E-02   5.32E-07
     81    39.00    82.00    41.63   1.33E-01   2.09E-06
     82    26.00    56.00    26.57   1.81E-01   3.82E-06
     83    13.00    96.00    33.24   2.21E-01   5.57E-06
     84    37.00    65.00    36.00   8.86E-02   1.03E-06
     85    20.00    97.00    37.22   1.71E-01   3.55E-06
     86    11.00    56.00    13.45   3.33E-01   9.32E-06
     87     3.00    89.00    24.08   1.85E-01   3.19E-06
     88    24.00    97.00    39.41   1.35E-01   2.19E-06
     89     6.00    59.00     7.81   5.41E-01   1.96E-05
     90     3.00    46.00    19.10   1.47E-01   1.89E-06
     91    31.00    59.00    30.59   1.46E-01   2.92E-06
     92    43.00    66.00    42.01   7.03E-02   5.60E-07
     93    38.00    82.00    40.72   1.35E-01   2.20E-06
     94    42.00    74.00    41.98   1.02E-01   1.24E-06
     95    29.00    48.00    32.76   2.26E-01   6.95E-06
     96    16.00    88.00    27.46   1.02E-01   1.06E-06
     97    40.00    57.00    39.81   1.42E-01   2.98E-06
     98    23.00    89.00    32.56   7.69E-02   6.56E-07
     99    14.00    48.00    21.40   2.02E-01   3.97E-06
    100     6.00    81.00    16.76   2.18E-01   3.79E-06
    101    27.00    89.00    35.38   6.55E-02   4.96E-07
    102     9.00    51.00    16.12   2.34E-01   4.88E-06
    103     2.00    76.00    11.05   4.20E-01   1.29E-05
    104    34.00    51.00    35.85   2.10E-01   6.45E-06
    105     4.00    57.00     8.54   5.34E-01   1.86E-05

1-There is no (inFile,product)??
2-while (getline -->will skip the whole file not the comment

//skip the comments
	while (getline(inFile,product))
	{
	   if (product[1] = '!')
   	   cout << product << '\n';
	}

thanks for the quick reply

1-There is no (inFile,product)??

you are quite correct, my quickly thrown together example from several attempts was supposed to be

while (getline(vis2_file,product))

and I had removed the

string product;

line whilst trimming the fat from my example. sorry

2-while (getline -->will skip the whole file not the comment

//skip the comments
	while (getline(inFile,product))
	{
	   if (product[1] = '!')
   	   cout << product << '\n';
	}

ahh. not intended. I was hoping I could loop over lines that contained comments then start reading the useful stuff using fscanf. Any suggestion how to read a line, and if comment or blank move on, then when it reaches something that is not a comment it changes to reading individual values in using fscanf?

Thanks again for the quick reply

Jimbo

Edited 5 Years Ago by jimbo_cambridge: n/a

just

if (product[0] = '!')
{
   cout << product << '\n';
   break;
}

Hi, so I tried a variation of this...

while (getline(vis2_file,product))
	{
	   if (product[1] == '!')
           {
           cout << product << '\n';
           } 
	   else
	   {
	   break;
	   }     
	}

which prints out the comments nicely, and seems to break in the right place - ie it does not print any of the non-commented lines. The problem is that the following fscan lines do not seem to pass the text values to the arrays - when I cout any of the values I get the crazy small numbers associated with undesignated variables.

As I understand it, the fscanf command should scan through the file (starting where my break statement is??) ignoring whitespace and linebreaks to read in any characters (in the format specified.) This sounds exactly right for my application, but there must be a subtelty that I am missing.

Thanks again for the help

Jimbo

Edited 5 Years Ago by jimbo_cambridge: n/a

I should add, I had to remove the

if (vis2_file.is_open())

as the break command exits this as well (I think)

Right, thanks to your direction, I have a working example...

double NUV;
double U0;
double V0;
double ZEROSP;
double BGS1;
double BGS2;
double INTCPT;
double SLOPE;

//VIS2 - Works for .calvis data
  string holder;
  string comment;
  string sNUV, sU0, sV0, sZEROSP, sBGS1, sBGS2, sINTCPT, sSLOPE;
  string svis2id, sucoord, svcoord, sr, svis2_data, svis2_err;
  ifstream vis2_file("/home/jimbo/UKIRT/cah_reduction_ukirt97/N2/10216K/10216.calvis");

    while (getline(vis2_file,comment))
    {
      if (comment[1] == '!')
      {
         cout << comment << '\n';
      } 
      else
      { 
        sNUV = comment;
        break;
      }     
    }

    //read in header data
    vis2_file  >> sU0 >> sV0 >> sZEROSP >> sBGS1 >> sBGS2 >> sINTCPT >> sSLOPE;
    //convert to numbers
    NUV = atof ( sNUV.c_str() );
    U0 = atof ( sU0.c_str() );
    V0 = atof ( sV0.c_str() );
    ZEROSP = atof ( sZEROSP.c_str() );
    BGS1 = atof ( sBGS1.c_str() );
    BGS2 = atof ( sBGS2.c_str() );
    INTCPT = atof ( sINTCPT.c_str() );
    SLOPE = atof ( sSLOPE.c_str() );

    int Nvis2 = int(NUV);
    Row<double> vis2id(Nvis2);
    Row<double> ucoord(Nvis2);
    Row<double> vcoord(Nvis2);
    Row<double> r(Nvis2);
    Row<double> vis2_data(Nvis2);
    Row<double> vis2_err(Nvis2);
 
    for(int i = 0; i < Nvis2; i++)
    {
      //read in data
      vis2_file >> svis2id >> sucoord >> svcoord >> sr >> svis2_data >> svis2_err;
      //convert to numbers
      vis2id[i] = atof ( svis2id.c_str() );
      ucoord[i] = atof ( sucoord.c_str() );
      vcoord[i] = atof ( svcoord.c_str() );
      r[i] = atof ( sr.c_str() );
      vis2_data[i] = atof ( svis2_data.c_str() );
      vis2_err[i] = atof ( svis2_err.c_str() );
    }  

    vis2_file.close();

The only problem (apart from being ugly) is that it assumes that the first line of header data only contains one number (which is true in the case of this particular file but may not be in future).

The problem is that I loose a line when the comments checker performs a getline and it is decided that the line is not a comment.

I guess there is a way to count the number of comment lines, and then restart the file input skipping that number of lines. That way I could start the >> in the right plase and read in the first variable I need

Thanks

Jimbo

This article has been dead for over six months. Start a new discussion instead.