Flush Stream

Hello everybody, I´m reading a text file with words, and something strange happens.

Suppose that I´m reading the following words (this words are in the text file):

cebolla
yuca

When I opened this words with the getline method that is in the stream class, I got this:

cebolla
yucalla

As you can see, the word yuca take the last tree letter of the word cebolla. Someone knows why this is happening? I guess that I did not flush the stream.

Best Regards.
Can you post the code that you use to read from the file?
Sure:

1
2
3
4
5
6
7
8
9
ifstream archivo ("sopas.txt");
      while(elArchivo.getline(lista, longitud)) {
         Cadena palabra;
         palabra.setCadena(lista);
         this->setPalabraLista(palabra,contador);
         //cin.clear();
         contador++;
      }
I don't know if I understand the problem. After calling getline(), lista should contain some characters followed by a null character ('\0'). I can only give you a general advice:

Instead of this:
1
2
char lista[longitud];
while (elArchivo.getline(lista, longitud))


try this:
1
2
std::string lista;
while (std::getline(elArchivo, lista))


And do the same everywhere you're using char[] or char*. Replace it with std::string.
Topic archived. No new replies allowed.