简体   繁体   English

从文件读取数据时发生无限循环

[英]Infinity loop while reading data from file

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);
    } 

Problems that I see. 我看到的问题。

  1. You are using the wrong format, %fl instead of %lf . 您使用了错误的格式, %fl而不是%lf
  2. You are not checking whether fscanf successfully read the data or not. 您没有检查fscanf是否成功读取数据。

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
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM