0

I have an input file in which the first column is made by string and the rest are numeric values.

E2   1880    1   0   67.50   10.50   -1.00   -1.00
E2   1880    1   4   66.50   11.50   -1.00   -1.00
E2   1880    1   8   66.50   11.50   -1.00   -1.00
E2   1880    1   12      65.50   11.50   -1.00   -1.00
E2   1880    1   16      64.50   11.50   -1.00   -1.00
E2   1880    1   20      63.50   12.50   -1.00   -1.00
E2   1880    2   0   63.50   12.50   -1.00   -1.00
E2   1880    2   4   62.50   12.50   -1.00   -1.00
E2   1880    2   8   62.50   12.50   -1.00   -1.00

The problem is that I need to store the input file in a 2d array but when I try to do it I get only 0 and I suspect it is because in the first column there are not numeric values..

Here the code

sprintf(FILE,"test.dat");
IN.open(FILE,ios::in);
if(IN.is_open()){
    while ( IN.good() ) {
        getline (IN,line);
        ++data;
    }   
    data -= 1;
}
IN.clear();
IN.close();

double** buf;
buf = new double* [data];
for(int k=0;k<data;k++) buf[k] = new double[COL];
for(int k=0;k<data;k++){
    for(int j=0;j<COL;j++) buf[k][j] = 0.;
} 

sprintf(FILE,"test.dat");
IN.open(FILE,ios::in);
if(IN.is_open()){   
for(int j=0;j<data;j++){
    for(int k=0;k<COL;k++){ 
        IN >> buf[j][k];    
    }
}
IN.clear();
IN.close();

Many thanks!

5
  • 2D array skipping E2 & 1880 and including rest of elements ? Commented Sep 5, 2013 at 16:56
  • buf[j][k]; where is this initialized what properties(string,int, object ect.) does it have? Commented Sep 5, 2013 at 16:58
  • Is buf declared as a double array? Commented Sep 5, 2013 at 16:59
  • Missing code, where did buf from? Commented Sep 5, 2013 at 17:00
  • Using sprintf from <cstdio> and a variable named FILE is confusing, since FILE is a type from the C standard library. You might want to avoid using all-caps variables altogether. Commented Sep 5, 2013 at 17:00

3 Answers 3

2
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;

int main()
{
    vector<vector<double>> data;
    ifstream file("test.dat");
    string line;
    while (getline(file, line))
    {
        data.push_back(vector<double>());
        stringstream ss(line);
        // skip first column
        { string temp; ss >> temp; }
        double value;
        while (ss >> value)
        {
            data.back().push_back(value);
        }
    }

    for (int y = 0; y < data.size(); y++)
    {
        for (int x = 0; x < data[y].size(); x++)
        {
            cout << data[y][x] << " ";
        }
    }
    cout << endl;
}
Sign up to request clarification or add additional context in comments.

Comments

1

First of all this will save just the last line in your line

while ( IN.good() ) {
    getline (IN,line);
    ++data;
} 

And I don't think that is what you want.

In order to store you data from the file in the array just use

int j = 0;
int k = 0;
std::string discard;
IN >> discard; // discard first "E2"
while (IN >> buf[k][j]){
     if (j < COL-1) j++;
     else if (k < data){  // whenever j reaches COL - 1
         IN >> discard;   // next element (which is "E2" is discarded)
         j = 0;           // col nr reseted
         k++;             // row nr incremeted
     }
}

This code assumes the column containing "E2" is not counted.

Comments

0

Your while loop should be changed to:

while ( std::getline(IN, line) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.