I am trying to read a file with a lot of data and store this data in a map with vectors. The file has one line of strings as the first line, which I want to use as keys, and all the other lines are data that I want to store in the vector<double>.
I read the file twice. The first time to store all the data (accept for the strings in the first line) in a vector "temp". The second time I sort the data in "temp" according to the key they belong to. I think the problem lies there, because the pogram prints "name" in the "for (int line)" - loop but then I get a segmentation violation..
#include <iostream>
#include <sstream>
#include <string>
#include <string.h>
#include <fstream>
#include <vector>
#include <map>
int main()
{
constchar * data_file = "testWPS.txt" ;
int NoC = 1259 ;
int NoL = 167 ;
vector<double> temp ; // vector with all the data of the file
double value ;
// store the data in v_temp, v_temp is read out later
ifstream file0(data_file) ;
if (file0.is_open()) {
for (int header = 0; header < NoC+1; header++) {
file0 >> value ;
} // end for header
while (file0.good()) {
file0 >> value ;
temp.push_back(value) ;
if( file0.eof() ) { break ; }
} // end while good
file0.close() ;
} // end if open
else { cout << "Unable to open data file!" << endl ; }
map< string, vector<double> > WPS ;
string name ;
// store the data of v_temp with a key in the WPS map
ifstream file1(data_file) ;
if ( file1.is_open()) {
for ( int column = 0; column < NoC; column++){
file1 >> name ;
for (int line = 0; line < NoL; line++) {
cout << name << endl ;
WPS[name].push_back(temp[line*NoC+column]) ;
} // end for line*/
} // end for column
file1.close() ;
} // end if open
else { cout << "Unable to open data file!" << endl ; }
vector<double> wps_pbb_c_ux = WPS["WPS_PBB_C_UX"] ;
for (int i = 0; i < 1259; i++) {
cout << "Value " << i << ": " << wps_pbb_c_ux[i] << endl ;
}
return 0 ;
}
Thanks for the help. I checked the size of the temp vector and it is zero, so that might be causing the problem. I am going to look into it further because I don't know why it is zero. I think there is something wrong with the while(file0.good()) statement because when I use a cout in the loop, nothing is printed on the screen.
1 2 3 4 5 6 7 8
while ( file0.good() ) {
file0 >> value ;
temp.push_back(value) ;
cout << value << endl ; // nothing is printed..
if( file0.eof() ) { break ; }
} // end while good
The reason I read the file twice is because first I want to store all the data in a temporary file, so I skip the first line with strings
1 2 3 4 5 6
for (int header = 0; header < NoC+1; header++) {
file0 >> value ;
cout << value << endl ;
} // end for header
And then during the second time I just read the first line, storing the strings as keys, and picking out the right data point from temp.
Yeah there is probably a way to read it once, but I can't figure it out.
The problem I have is that in one line, there is a value for every key. So there are "the number of columns" amount of values in one line and each value in that line belongs to a different key.
So the only way I could do it was by reading the file twice but if you think of a different approach I would love to read it :)
map< string, vector<double> > Map;
ifstream file(file_name);
if (file.is_open())
{
while (key.compare("END") != 0)
{
file >> key;
} // end while not END
for (int CL = 0; CL < NoC*NoL; CL++)
{
file >> data;
Vec[CL] = data;
}
file.close();
// insert your data here.
Map.insert(pair<string, vector<double>>(key, Vec));
} // end if open
else { cout << "Unable to open file!" << endl; }
I tried this method but I get a segmentation violation instantly. I searched on the internet and I can only find examples of the use of insert if an int or a double in paired up with a key.
Vec contains all the unsorted data, don't you think I have to sort it first?
So I am lot further now, but when I want to manipulate the data, and plot it eventually, the code seems to become way more simple if I make a map of doubles[], but that is another thing I can't solve. So I changed my code to this:
// storing Data from file_name in a Map
void getMapArr (constchar * file_name, map<string, double[]> &Map) {
int NoC = NumberOfColumns(file_name) ;
int NoL = NumberOfLines(file_name) ;
double * Complete_Data = newdouble[NoC*NoL] ;
string * keys = new string[NoC] ;
double data ;
string key ;
ifstream file (file_name) ;
if ( file.is_open() ) {
for ( int i = 0; i < NoC; i++ ) {
file >> key ;
keys[i] = key ;
} // end for i
for ( int i = 0; i < NoC*NoL; i++ ) {
file >> data ;
Complete_Data[i] = data ;
}
file.close() ;
} // end if open
else { cout << "Unable to open file!" << endl ; }
for ( int i = 0; i < NoC; i++ ) {
double * temp_data = newdouble[NoL] ;
for ( int j = 0; j < NoL; j++ ) {
temp_data[j] = Complete_Data[i+j*NoC] ;
} // end for j
Map.insert( pair<string, double[]>(keys[i], temp_data) ) ;
delete temp_data ;
} // end for i
} // end void getMap
And now I get the error: no matching constructor for initialization of
'pair<string, double []>'
Map.insert( pair<string, double[]>(keys[i], temp_data) )
So now I don't open the file twice that's and improvement, but I want to store the columns in arrays of doubles and that I can't do..
your sample data columns don't match
you have 11 string column header but your data has only 10 columns.
i just placed dummy data at the end so that the columns match.
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <fstream>
usingnamespace std;
// storing Data from file_name in a Map
void getMapArr (string file_name, map<string, vector<double>> &Map) {
int NoC = 11; // i don't have your number of column function.
int NoL = 6; // i don't have your number of lines function.
// avoid using new if possible use vectors instead for arrays.
vector <double> Complete_Data;
vector<string> keys;
double data ;
string key ;
ifstream file (file_name.c_str()) ;
if ( file.is_open() )
{
// read keys
for ( int i = 0; i < NoC; i++ )
{
file >> key ;
keys.push_back(key);
} // end reading keys
// read all the data
for ( int j= 0; j < (NoC * NoL); j++ )
{
file >> data ;
Complete_Data.push_back(data);
}// end reading data.
file.close() ;
} // end if open
else
{
cout << "Unable to open file!" << endl ;
}
vector<double> temp;
for ( int i = 0; i < NoC; i++ )
{
for ( int j = 0; j < NoL; j++ )
{
// store in buffer
temp.push_back(Complete_Data[i+j*NoC]);
}
// insert in map
Map.insert( pair<string, vector<double>>(keys[i], temp)) ;
// clear temporary buffer
temp.clear();
} // end for i
} // end void getMap
int main()
{
string filename("sample.txt");
map< string, vector<double> > WPS;
getMapArr(filename,WPS);
// test output
string name = "END";
// output the value associated with the string.
for (int i = 0; i < WPS[name].size(); i++)
{
std::cout << WPS[name][i] << std::endl;
}
int x;
cin >> x;
return 0;
}