0

I'm trying to read data from file. There are three rows. What I've done is this below. Problem is that (file exists) it is infinity loop while reading a file. I've observed that program is not moving line by line until it reaches end of file. What's incorrect in my 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 is printed infinitely.
        }
            fclose(desktops);
    } 
Anthon
  • 69,918
  • 32
  • 186
  • 246
Coder
  • 13
  • 1

1 Answers1

0

Problems that I see.

  1. You are using the wrong format, %fl instead of %lf.
  2. You are not checking whether fscanf successfully read the 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 with error condition
 }
R Sahu
  • 204,454
  • 14
  • 159
  • 270