Ok.I did it otherwise! getline() for the WIN ! :D So this is my code (this time is much more readable) :
bool readNextString(ifstream &file , char pBuffer[] )
{
while(isWhitespace(file.peek()) && !file.eof())
file.ignore(1);
if(!file.eof())
{
streamoff start = file.tellg();
stringstream toComma;
if(file.getline(pBuffer , 200 , ','))
{
toComma << pBuffer;
toComma.getline(pBuffer ,200, '\n');
int i=strlen(pBuffer)-1;
for(; isWhitespace(pBuffer[i]) ;i--)
;
pBuffer[i+1] = '\0';
file.clear();
file.seekg(start + strlen(pBuffer) , file.beg);
return true;
}else return false;
}
return false;
}
I've made some changes in my other function too:
void printCities()
{
ifstream city ;
city.open("cities.txt", fstream::in);
if(city.is_open())
{
if(!isEmpty(city))
{
char currCity[200];
int counter = 1;
while(readNextString(city , currCity) && counter < 10)
cout << counter++ << ". " << currCity << endl;
}else
cout << "There are no cities added.\n";
city.close();
}else
cout << "Error by opening 'cities.txt'.Make sure that the file exist and try again.\n";
}
The function isEmpty(ifstream &file)
returns true if the file is empty and false otherwise.
Thanks to all for help ! Best regards !