Hi guys. I am studying engineering and have to do a project that calls in a csv file. I have never worked with any computer programming in my life but am kind of enjoying it.
I have to make it into a 2-D matrix or array, and all the entries separated by a comma (it being a csv file and all:) )are the entries in the array. But i must also use the characters that are null spaces.
I tried using the example that our lecturer gave us and he used something called 'buffer' (I dont know if this is something that C++ uses or is just something he picked?) and it worked for another tut that I did in one of our tut sessions but not this one. I am checking to see if the entries in the buffer array are not equal to a comma, and storing (or trying to anyway) that into another array.
Or I can use the vector < vector < float > > function, but am stumped as to how import the file into this 2-D vector. I tried using a void, but the only one I can even think of using with my pathetic knowledge is used with the buffer function. it goes like this:
void getfile (char filename [FILENAME_MAX], float tunneldata [][]) (tunneldata is what I am calling the file in the code.)
But the code that we used in our tut used that buffer thing and buffersize, and I don't know if and how to use this if I use the vector instead.
I know this is probably very simple but I am a complete idiot when it comes to computers and need help please?
#include <deque>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
//----------------------------------------------------------------------------
// Notice how the data is stored as a 2D deque. This accomodates huge files.
// I could use a vector instead if I also know the input files are small.
//
struct data_t: deque <deque <float> >
{
typedef deque <deque <float> > ::iterator record_iterator;
typedef deque <float> ::iterator field_iterator;
bool load( const string& filename );
bool save( const string& filename );
bool save( ostream& outs );
};
//----------------------------------------------------------------------------
bool data_t::load( const string& filename )
{
string s;
ifstream f( filename.c_str() );
while (getline( f, s ))
{
deque <float> record;
istringstream iss( s );
while (getline( iss, s, ',' ))
{
float fieldvalue = 0.0f;
istringstream( s ) >> fieldvalue;
record.push_back( fieldvalue );
}
this->push_back( record );
}
return f.good();
}
//----------------------------------------------------------------------------
bool data_t::save( const string& filename )
{
ofstream f( filename.c_str() );
if (!f) returnfalse;
return save( f );
}
//----------------------------------------------------------------------------
bool data_t::save( ostream& outs )
{
for (data_t::record_iterator ri = this->begin(); ri != this->end(); ri++)
{
for (data_t::field_iterator fi = ri->begin(); fi != ri->end(); fi++)
outs << ((fi == ri->begin()) ? "" : ", ") << *fi;
outs << endl;
}
return outs.good();
}
//----------------------------------------------------------------------------
int main()
{
data_t data;
string filename;
cout << "File to read> ";
getline( cin, filename );
data.load( filename );
cout << "The data is:\n";
data.save( cout );
return 0;
}
The trick is using getline() do do all the dirty work for you: it gets one record at a time (line 26), then separates the record into fields (line 30).
Records don't have to have the same number of fields. Be sure to access fields using the at() member function or by the iterators I gave you.
Also be sure to tell your lecturer where and how you learned to do this kind of thing.