Hi there,

I am new to the forum. I know this topic is not new. I have did a search on it and did not quite get what I wanted. Currently, I am trying to write a c++ programme which help me to read in about 10000 rows of numerical data, with 4 columns in each rows. There is some words before the numerical data and those should be discarded by the programme.
I wanted to import the data into my c++ programme and fit those data into a 2x2 vector(to store all the data).

I have 2 problems:

1) the number of row of my data are not constant and I don't have any idea how to dynamically insert the data into my vector(change my vector row according to rows number).

2) I have written a simpler version of my code to proccess a simpler version of data file(attached with this). But it does not compile(can't get fgets to work). Any idea?

Thanks for your help in advanced.

Anyway, here's my code.

using namespace std;

int main(int argc,char* argv)
    int i,j=4,k;
    char* pend;
    char line[256];
    double d[j];
    vector< vector<double> > data; //define a vector called data
    //Read in files from folder
    //Need to expand to multiple file input

    ifstream is; //define object which reads in from a file
    is.open("C:\\Research\\TGA\\processed data\\test.txt"); //open file
    //if file cannot be opened
        //print error and exit
        cerr<<"file cannot be opened."<< endl;
            fgets(line,256,is); //read lines from is to line
            d[0]>>strtod(line,&pend); //convert char to double var
            [INDENT]while(d[0]>0.0){ // check if d[0] is a number, if not get the data out of loop
                    [INDENT]for(i=0; i<j; i++)//loop for creating vector rows
                        [INDENT]for(k=0; k<j; k++)
                        data[i].push_back(d[i]);//add vector columns to rows}
    return 0;

Recommended Answers

All 3 Replies

Read the entries in using the extraction operator >> (so you don't have to convert from text to numbers, just read right into doubles)

I read the text file, but are you saying that there is:

Text  1.0 2.0 3.0 4.0
Text2 etc.

because you can extract the text first and then throw it away.

In terms of the variable rows, you can simply use the .push_back() method of the vector and you won't have to keep track.

fgets is a great function, but it works with C-style FILE* pointers instead of ifstrea

yes, there's always 4 columns of data(row of data is not always a constant). However, the row is either consist entirely alphabetical words or numerical data. I just want the numerical bits(as the attached file showed). Will you recommend me to read in the alphabetical text as well?

if fgets is not a good option, will getline be a better option?

Again, your data are ideal for using ">>" (with getline you have to do all of the parsing to get the numbers back)

string throwaway;
vector<vector<double> > data
while(is >> throwaway)
      for (over the vector indexes)
         Read into a double using >>, push_back onto 1D vector (one for first row of the 2D vector, one for the second)
      Push 2 1D vectors into 2D vector "data"

(easier if you represent your 2D vector by a 1D vector with 4 elements)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.