c++ - list not displaying from txt file -
void main() { nodlista* ls=null; file* f=fopen("asaceva.txt","r"); if(f!=null) { char buffer[100]; int id;float pret; fscanf(f,"d",&id); while(!feof(f)) { fscanf(f,"f",&pret); fscanf(f,"s",buffer); produs* p= creareprodus(id,pret,buffer); ls=inseraresfarsit(ls,*p); fscanf(f,"%d",&id); } afisarelista(ls); } _getch(); }
afisarelista
: displays listinseraresfarsit
: inserts @ end
i don't understand why doesn't data txt file. can explain why?
there couple of issues in code:
- main not returning integer.
- not using fscanf consistently , correctly placeholders.
- you not checking return value of fscanf failure.
- null should replaced nullptr if have c++11 support available.
the correct code should this:
int main() { nodlista* ls=null; file* f=fopen("asaceva.txt","r"); if(f!=null) { char buffer[100]; int id;float pret; if (!fscanf(f,"%d",&id)) cout << "error happened: " << ferror(f) << ", error string: " << strerror(errno) << endl; while(!feof(f)) { if (!fscanf(f,"%f",&pret)) cout << "error happened: " << ferror(f) << ", error string: " << strerror(errno) << endl; if (!fscanf(f,"%s",buffer)) cout << "error happened: " << ferror(f) << ", error string: " << strerror(errno) << endl; produs* p= creareprodus(id,pret,buffer); ls=inseraresfarsit(ls,*p); if (!fscanf(f,"%d",&id)) cout << "error happened: " << ferror(f) << ", error string: " << strerror(errno) << endl; } afisarelista(ls); } _getch(); return 0; }
Comments
Post a Comment