Hello. There is a task to read integer numbers from a file and add to the end of a single-linked linear list. I implement reading:

void createListFromFile(List &start) { ifstream f("f.txt"); deleteList(start); List p = new Node; start = p; while(!f.eof()){ f >> p->inf; p->next = new Node; p = p->next; } p->next = NULL; f.close(); } 

I realize the seal:

 void printList(List start) { if (!start) cout << endl << "Список пуст!"; else while (start) { cout << start->inf << "\t"; start = start->next; } cout << endl; } 

As a result, if there are n numbers in the file, then n + 1 is printed on the screen and is necessarily left, for example -842150451. It's clear that he either reads one extra empty number and assigns it an empty (random) value or prints an extra one. But I can not find a mistake. I ask for your help. Thank.

  • @embarcadero, with eof, you were prompted (and I, of course, understand that the test program), but if you checked the success of entering a number (what if there was a letter instead of a digit?), you would have seen the error yourself before. - avp

3 answers 3

A very common mistake. Here it is the wrong check of the end of the file. The eof flag is not set when the last byte from the file is read, but after an attempt is made to read outside the file. Those. you read the last element, eof == false, then try to read outside the file, eof == true, it is not known what is being read, it is listed, and only then the cycle is interrupted.

How to do. Check eof immediately after reading and before listing the item, for example, like this.

 while(true){ f >> p->inf; if (f.eof()) break; p->next = new Node; p = p->next; } 

Or as written here .

In connection with the absolutely fair remark of @avp, the whole function should be rewritten something like this

 void createListFromFile(List &start) { ifstream f("f.txt"); if (!f.good()) return; //Можно выкинуть исключение deleteList(start); int temp; f >> temp; if (f.eof()) { start=NULL; //Если deleteList не обнуляет start f.close(); return ; } List p = new Node; p->inf = temp; start = p; while(true){ f >> temp; if (f.eof()) break; p->next = new Node; p = p->next; p->inf = temp; } p->next = NULL; f.close(); } 

If where wrong, sorry, an hour late.

  • @mikillskegg, but still in the last element will be porridge. Node should be added to the list not before, but after a successful reading. - avp
  • Well, what about me? - skegg
  • I agree with @avp. And because the element is first stitched to the list, and then the inf variable is filled with the value from the file. - gecube
  • Although in practice there is no cereal. My thanks for one example, the second one discards the last true number) - embarcadero
  • I do not know. I checked, I read everything correctly. Where is your mistake elsewhere? - skegg

The problem is clear. See, after all, after reading each number, you add a new Node , including after the last one. In this Node number will, of course, be wrong.

    Try peeping into the next character with peek (). Then the recommendations of the distinguished @avp to check the symbol can be performed.