Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;while循环问题_C++_Struct_While Loop_Getline - Fatal编程技术网

C++ C++;while循环问题

C++ C++;while循环问题,c++,struct,while-loop,getline,C++,Struct,While Loop,Getline,我正在尝试将文件的行(cityName,hiTemp,loTemp)读入结构数组。我能够使用>阅读前几行,直到我击中一个城市,城市名称中有一个空格 然后,我尝试使用getline()读取这些行,但随后我的while循环停止工作 我不知道为什么会这样 int LoadData() { int count = 0; string path; cout << "Specify the input file path: "; ifstream inFile; cin >> p

我正在尝试将文件的行(
cityName
hiTemp
loTemp
)读入结构数组。我能够使用
>
阅读前几行,直到我击中一个城市,城市名称中有一个空格

然后,我尝试使用
getline()
读取这些行,但随后我的while循环停止工作

我不知道为什么会这样

int LoadData()
{
int count = 0;
string path;


cout << "Specify the input file path: ";
ifstream inFile;
cin >> path;

inFile.open(path.c_str());

if (!inFile.is_open())
{
    cout << "Error - could not open file: " << path;

    return (-1);
}

else
{
    while (!inFile.eof())
    {

        cities[count].city = "";
        getline(inFile, cities[count].city);

        if (cities[count].city.length() == 0)
        {
            break;
        }

        char comma;
        inFile >> (cities[count].high) >> comma >> cities[count].low;
        cout << cities[count].city << " " << cities[count].high << " " << cities[count].low << endl;

        count++;

    }

    inFile.close();
    inFile.clear(std::ios_base::goodbit);
    return count;
}


}
int-LoadData()
{
整数计数=0;
字符串路径;
路径;
infle.open(path.c_str());
如果(!infle.is_open())
{
cout(城市[count]。高)>>逗号>>城市[count]。低;

不能使用
getline
作为循环条件。也可以用
getline
替换第二次读取,并使用
stringstream
对其进行解析

#include <sstream>
// ...
while(getline(inFile, cities[count].city)) {
  if (cities[count].city.empty()) break;
  // read next line with high and low values
  string str;
  if (!getline(inFile, str)) break; // error in file format
  stringstream ss(str);
  char comma;
  ss >> cities[count].high >> comma >> cities[count].low; // parse it
}
#包括
// ...
while(getline(infle,cities[count].city)){
如果(cities[count].city.empty())中断;
//读取下一行的高值和低值
字符串str;
如果(!getline(infle,str))中断;//文件格式错误
stringstream ss(str);
字符逗号;
ss>>城市[count]。高>>逗号>>城市[count]。低;//解析它
}

使用
getline
作为循环条件。也可以使用
getline
替换第二次读取,并使用
stringstream
对其进行解析

#include <sstream>
// ...
while(getline(inFile, cities[count].city)) {
  if (cities[count].city.empty()) break;
  // read next line with high and low values
  string str;
  if (!getline(inFile, str)) break; // error in file format
  stringstream ss(str);
  char comma;
  ss >> cities[count].high >> comma >> cities[count].low; // parse it
}
#包括
// ...
while(getline(infle,cities[count].city)){
如果(cities[count].city.empty())中断;
//读取下一行的高值和低值
字符串str;
如果(!getline(infle,str))中断;//文件格式错误
stringstream ss(str);
字符逗号;
ss>>城市[count]。高>>逗号>>城市[count]。低;//解析它
}
要获取文件中的每一行,应使用:

while(getline(inFile, cities[count].city)) {
    // ...
这是可行的,建议使用
.eof()
方法

您也可以在if语句中使用此选项:

if (!getline(inFile, str)) 
    break;

另外,您可以阅读本网站:

-堆垛溢流柱

它深入了解了为什么在while循环中使用
.eof()
不是检查是否已到达文件结尾的首选方法

要获取文件中的每一行,应使用:

while(getline(inFile, cities[count].city)) {
    // ...
这是可行的,建议使用
.eof()
方法

您也可以在if语句中使用此选项:

if (!getline(inFile, str)) 
    break;

另外,您可以阅读本网站:

-堆垛溢流柱


它深入了解了为什么在while循环中使用
.eof()
不是检查是否已到达文件结尾的首选方法。

@DanielFeasler您是否从while()中取出了getline()循环?因为你有它在你的情况下,你不需要它在你的while循环中。同样,我无法从评论部分阅读你的代码。请更新你的问题帖子@DanielFeasler。
infle>>(城市[count].high)>>逗号>>城市[count].low;
为什么有这一行?如果你正在阅读这一行,我试图阅读的文件是一个高温和低温城市的列表。如下:
Katmandu,-34,28
@DanielFeasler getline()从文件中读取整行。因此,如果你在该文件的一行中有“Katmandu,-34,28”,那么
城市[计数].city
应该保存该文件中的整行。@DanielFeasler您是否从while()循环中取出了getline()?因为您的情况下有getline(),while循环中不需要它。同样,我无法从评论部分读取您的代码。请更新您的问题帖子@DanielFeasler。
infle>>(cities[count].high)>>逗号>>城市[count].low;
为什么有这一行?如果您正在阅读这一行,我试图阅读的文件是一个具有高温和低温的城市列表。如下所示:
Katmandu,-34,28
@DanielFeasler getline()从文件中读取整行。因此,如果您有“Katmandu,-34,28”在该文件的一行中,
cities[count].city
应保存该文件中的整行内容。