Ok, I thought there were spaces between the cities.
Here's the code that will work:
Code:
#include <list>
#include <string>
#include <fstream>
using namespace std;
char ch;
ifstream fin ( " myfile.txt" );
list<string> cities;
string tmp_str;
while( !fin.eof() )
{
fin.read(&c,1);
if( c == ',')
{
tmp_str.erase( tmp_str.begin() ) ; // erase the first character ( the opening quote )
tmp_str.erase( tmp_str.end() ) ; // erase the lase character ( the closing quote )
cities.push_back( tmp_str );
tmp_str.clear();
}
tmp_str = tmp_str + c;
}
Example usage of those strings :
Code:
for( list< string >::iterator ii = cities.begin() ; ii != cities.end() ; ii++ )
cout << *ii << endl; // print the current city
Bookmarks