0
struct list
{
    char town[30];
    int code;
    spisok *next,*head;
};

void reading(list *x)
{
    ifstream out("C:\\Users\\Tkachenko\\Desktop\\mp\\Lab_5\\file",ios::binary);
    if (out)
    {
        while(!out.eof())
        {
            out.read((char*)&x->town,sizeof(x->town));
            out.read((char*)&x->code,sizeof(x->code));
            x=x->next;
        }
        out.close();
    }
    else
        cout << "\n404 not found...";
}

I have issues with reading from a file. It writes additional lines that are repeats of previous lines. How can I fix that?

Brian
  • 14,610
  • 7
  • 35
  • 43
Bogdan Tkachenko
  • 378
  • 1
  • 3
  • 11
  • Common failure: Not testing the stream state (or an unformatted input). Please get informed. –  May 27 '15 at 17:13
  • i forgot add in cycle cout << temp->town << "\t"; cout << temp->code << "\n"; – Bogdan Tkachenko May 27 '15 at 17:18
  • 1
    Not the actual issue but never use `!eof` to control a loop: http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – NathanOliver May 27 '15 at 17:19
  • i guess that issue but when i add adittional logical function exmp while(!out.eof() && x!=0) it works but when i work in program, when i use this function first time it crash program guess this issue is because my list is not declared – Bogdan Tkachenko May 27 '15 at 17:28
  • @BogdanTkachenko ...or even *allocated*. Loading a linked list usually involves performing node allocations for the list *during* the load; not before. – WhozCraig May 27 '15 at 17:59

0 Answers0