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.

#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;

int main(int argc,char* argv)
{
   [INDENT]
    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
    [INDENT]if(!is)
    {
        //print error and exit
        cerr<<"file cannot be opened."<< endl;
        exit(1);
    }
    else
    {
        k=0;
        while(is)
        {
            fgets(line,256,is); //read lines from is to line
            d[0]>>strtod(line,&pend); //convert char to double var
            d[1]>>strtod(pend,&pend);
            d[2]>>strtod(pend,&pend);
            d[3]>>strtod(pend,NULL);[/INDENT]
            [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}
                        cout<<data[i]<<endl;
                        }[/INDENT]
                    }[/INDENT]
    is.close();
        }
    return 0;
    }
[/INDENT]
[/INDENT]
}

Edited 5 Years Ago by WaltP: Somehow I can't get the code to indent the way I edit it. Sorry. -- Try using CODE tags. that's what they are for.

Attachments
Words should be discarded
Only read in numerical data.

10.0	20.0	30.0	40.0
50.0	60.0	70.0	80.0
321.1	90.0	321	313.1

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

Edited 5 Years Ago by jonsca: n/a

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?

Edited 5 Years Ago by majyun: n/a

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)

Edited 5 Years Ago by jonsca: n/a

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