user2287387
user2287387

Reputation: 47

Convert string to 2d array

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!

Upvotes: 0

Views: 1101

Answers (3)

Alexandru Barbarosie
Alexandru Barbarosie

Reputation: 2992

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.

Upvotes: 1

David G
David G

Reputation: 96810

Your while loop should be changed to:

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

Upvotes: 0

Neil Kirk
Neil Kirk

Reputation: 21763

#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;
}

Upvotes: 2

Related Questions