c - Infinity loop while reading data from file -
i'm trying read data file. there 3 rows. i've done below. problem (file exists) infinity loop while reading file. i've observed program not moving line line until reaches end of file. what's incorrect in code?
code:
if (desktops == null) { printf("\n no such file.\n"); } else{ printf("\nfile exists. reading\n"); while(!feof(desktops)){ if(numberofobjects== 0) { fscanf(desktops,"%fl %fl %fl %fl %d %s %s %d\n",&height,&length,&width,&processorclock,&idnumberserial,&processortypechars,&nameinnetworkchars,&id); nameinnetwork = string(nameinnetworkchars); processortype = string(processortypechars); // nameinnetwork = "test"; glowalistyobjektow = new desktop(height,length,width,processorclock,idnumberserial,processortype,nameinnetwork,id); iterator = glowalistyobjektow; iterator->previousobject = null; iloscobiektow++; nameinnetwork.clear(); processortype.clear(); } else if(numberofobjects> 0) { fscanf(desktops,"%fl %fl %fl %fl %d %s %s %d\n",&height,&length,&width,&processorclock,&idnumberserial,&processortypechars,&nameinnetworkchars,&id); nameinnetwork = string(nameinnetworkchars); processortype = string(processortypechars); // nameinnetwork = "test"; iterator->nextobject = new desktop(height,length,width,processorclock,idnumberserial,processortype,nameinnetwork,id); iterator->nextobject->previousobject = iterator; iterator = iterator->nextobject; iloscobiektow++; nameinnetwork.clear(); processortype.clear(); // nameinnetworkchars = null; } cout<<"reading line"<<endl; // here line above printed infinitely. } fclose(desktops); }
problems see.
- you using wrong format,
%fl
instead of%lf
. - you not checking whether
fscanf
read data or not.
change
fscanf(desktops,"%fl %fl %fl %fl %d %s %s %d\n", &height, &length, &width, &processorclock, &idnumberserial, &processortypechars, &nameinnetworkchars, &id);
to
int n = fscanf(desktops,"%lf %lf %lf %lf %d %s %s %d\n", &height, &length, &width, &processorclock, &idnumberserial, &processortypechars, &nameinnetworkchars, &id); if ( n != 8 ) { // deal error condition }
Comments
Post a Comment