PDA

Visualizza la versione completa : lettura file di dati .. problemi in lettura


drudox
05-10-2017, 14:29
Salve a tutti .. sto cercando di leggere dei dati da un file contenente delle linee di testo in cui e` nascosta la dimensione delle righe del blocco da leggere ... per capirci meglio qui ho incollato il file : http://dpaste.com/19HQY58

il programma che ho scritto per ora e` questo :




# include <iostream>
# include <fstream>
# include <iomanip>
# include <string>
# include <sstream>
# include <cstdlib>
# include <cmath>
# include <vector>
# include <exception>
# include <algorithm>
# include <cstring>


//----------------------------------------------------------------------------------------


using namespace std;


//--- Function prototipes


string readInput(int args, char* argv[],string& , string&);


void readData(vector<vector<vector<double>>>*, string& , int&);




//---


int main(int args, char* argv[]){


string time,root ;
string filename = "stangle.000000000.dat" ;
int N;


cout << "Processing " << filename << " ...." << endl;

vector<vector<vector<double>>>* ptrData ;
vector<vector<vector<double>>> TableData;



//cout << "ok" << endl ;

ptrData = new vector<vector<vector<double>>>(10);

readData(ptrData, filename, N);


int k=0;
cout << "--------------------" << endl;
//ptrData.resize(k)
// for(int i ; i < N ; i++ ){


// }
/*
cout << N << endl ;
for(int i=0; i< N; i++ ){
for(int j=0; j<3 ; j++){
cout << ptrData->at(k).at(i).at(j) << ' ' ;
}
cout << endl ;
}
*/
//
return 0;
}


//
//------


void readData( vector<vector<vector<double>>>* data, string& file, int& size){
unsigned int header = 3;
string tmp, row ;


ifstream inputFile;

cout << file << endl;
try
{
inputFile.open(file, ios::in);
}
catch(...)
{
cerr << "Error occurred opening file " << file << " program terminate!" << endl;
exit(1);
}

int indx=0;
vector<vector<vector<double>>> *vPtr ;
//vector<vector<vector<double>>> *
//data = new vector<vector<vector<double>>>(10);
int k=0;
int w=0;
getline(inputFile,row);
istringstream elem(row);
while(elem >> tmp);




// if(k==0)http://dpaste.com/19HQY58
while(!inputFile.eof() && k<=1){
while( w++ <= header-1 && getline(inputFile,row)){
istringstream elem(row);
if(w == header ){
while(elem >> tmp){
if(strcmp(tmp.c_str(),"I=" )== 0){
elem >> tmp ;
size = atoi(tmp.c_str());
}
}
}
}
cout << "size = " << size << endl;
//cout << size << endl;
//k=0;
data->resize(size*3) ;
data->at(k).resize(size*3) ;



for(int i=0; i<size; i++) // resize vector !
data->at(k)[i].resize(3) ;

int j=0;
while(getline(inputFile,row) && j < size){
istringstream elem(row);
for(int i=0; i < 3; i++)
elem >> data->at(k)[j].at(i);
elem >> tmp ; // 4th columns to skip
j++;
}

//vPtr->at(indx) = data;

for(int i=0; i < size ; i++ )
cout << data->at(k).at(i).at(0) << ' ' << data->at(k).at(i).at(1) << ' ' << data->at(k).at(i).at(2) << endl ;

k++ ;
}
}





il programma individua bene la prima sequenza .. trovando la ricorrenza di I= q assegnando quindi la dimensione che segue alla variabile size che mi permette di leggere esattamente il numero di righr che seguino ! dove sto sbagliando ?? vi ringrazio per l'aiuto

Loading