Hi *,

I have a file like this :

Example :
10 2
0 1 234.5677
0 2 345.6786
0 3 678.6764

From the first line the first number is number of lines and the second one is the number of nodes, so the thing is I need to extract those numbers first and then fill a matrix with the rest of the lines.

I can not extract just the first line get the numbers and initialiaze my matrix object and after fill it. I have the following code :

I read the file, get a line put it in a stream to split it and get the two numbers, aftet that I want to fill a matrix with the rest.

I am using a variable called lines that tells me if I am getting the first line or not, so if I am getting the first line I get the numbers and initialize my matrix object, if not I fill the matrix because I am reading the rest of the file.

I think :

1.- That is not the best way to do it
2.- It returns me

In member function ‘void Instance::getNewInstance(std::string)’:
Instance.cpp:78: error: ‘data’ was not declared in this scope

which is expected because I am initializing my matrix object inside the conditional (if) and filling it in the else case.
3.- Is there a way to read the first line and keep the pointer to that line in order to read the rest of the file inside a loop?. In python (for example) you can get the first line and after the rest of the lines inside a loop.

if (file.is_open()) // Open file operation succees
    while (! file.eof())  // to end of the file!
      getline(file,line); // read each line 
      stringstream ss(line); // put line in a stream
      while (ss >> buf) // reading putting each string in buf
        tokens.push_back(buf); // putting each value in a vector

      if (lines == 0) // this means first line n and m
        setN(atoi(tokens[0].c_str())); // n and m values are going to be used after
        int size = getN();
        MatrixHandler data(size,size);
      } else { // rest of the lines
        int from;
        int to;
        from = atoi(tokens[0].c_str());
        to = atoi(tokens[1].c_str());
        cout << "From " << setprecision(8) << from << " " << endl;
        cout << "To " << setprecision(8) << to << " " << endl;
        cout << "Cost " << setprecision(8) << atof(tokens[2].c_str()) << " " << endl;

      lines = 1;
    //cout << setprecision(8) << atof(tokens[2].c_str()) << endl;
    //cout << setprecision(9) << data.getMatrixValue(1,2) << endl;
  else cout << "File" << fileName << "can not be opened" << endl;

I hope to explain my case correctly.

Thanks in advanced and best regards.

Here is another way

if( file.is_open() )
    int n, x;
    float f;
    file >> n >> x; // get first line
    MatrixHandler data(n, n);
    while( file >> n >> x >> f ) // process remaining lines

        // fill in the matrix here


Wowww! Exactly!!.. It works very very good! Actually without that "tokens" stuff!... I have to thank you so much for guide me in the right way... While I learn more C++ I really enjoy it.

Thanks a lot!