However, this is a better way,
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
ifstream in("test.csv");
vector < vector <string> > data;
string element, delimiters = ",";
int row = 0;
char ch;
data.push_back( vector <string>() );
while( in.read( (char*)&ch, 1 ) )
{
if( ch == '\n' || ch == '\r' )
{
data.push_back( vector <string>() );
row++;
}
else if( delimiters.find_first_of(c
{
element += ch;
}
else
{
data[row].push_back( element );
element = "";
}
}
in.close();
for( unsigned int x = 0; x < data.size(); x++ )
{
for( unsigned int y = 0; y < data[x].size(); y++ )
{
cout << data[x][y] << ",";
}
cout << endl;
}
return 0;
}
Exceter
Main Topics
Browse All Topics





by: ExceterPosted on 2003-06-17 at 16:51:38ID: 8745149
You can use getline to read in each element. For example,
ifstream in("myfile.csv");
char val[100];
while( in )
{
in.getline( val, 100, ',' );
cout << val << endl;
}
in.close();
Exceter