从文件读取数据时无限循环
Infinity loop while reading data from file
我正在尝试从文件中读取数据。一共有三排。我所做的就是下面这个。问题是(文件存在)它在读取文件时是无限循环。我观察到该程序在到达文件末尾之前不会逐行移动。我的代码有什么不正确的地方?
代码:
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);
}
我看到的问题。
- 您使用的格式错误,
%fl
而不是 %lf
。
- 您没有检查
fscanf
是否成功读取数据。
改变
fscanf(desktops,"%fl %fl %fl %fl %d %s %s %d\n", &height, &length, &width, &processorClock, &idNumberSerial, &processorTypeChars, &nameInNetworkChars, &ID);
到
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
}
我正在尝试从文件中读取数据。一共有三排。我所做的就是下面这个。问题是(文件存在)它在读取文件时是无限循环。我观察到该程序在到达文件末尾之前不会逐行移动。我的代码有什么不正确的地方?
代码:
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);
}
我看到的问题。
- 您使用的格式错误,
%fl
而不是%lf
。 - 您没有检查
fscanf
是否成功读取数据。
改变
fscanf(desktops,"%fl %fl %fl %fl %d %s %s %d\n", &height, &length, &width, &processorClock, &idNumberSerial, &processorTypeChars, &nameInNetworkChars, &ID);
到
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
}