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]
}