>单一城市>>模板>>坦菲; 城市[指数]=单一城市; 低温[指数]=模板流; 高温[指数]=温度; 索引++; } cout,c++,arrays,fstream,C++,Arrays,Fstream" /> >单一城市>>模板>>坦菲; 城市[指数]=单一城市; 低温[指数]=模板流; 高温[指数]=温度; 索引++; } cout,c++,arrays,fstream,C++,Arrays,Fstream" />

分段故障核心转储并行阵列 我对C++非常陌生,我只是想读一个文本文件,它每行都有一个字符串和两个十进制数。当我试图将它们读入数组时,我会遇到一个分段错误……我仍在学习数组,不明白为什么这对我的一生都不起作用。代码如下: #include <iostream> #include <fstream> #include <string> using namespace std; const int ARRAY_SIZE = 1000; string fileLocation; ifstream newList; string city[ARRAY_SIZE]; double lowTemp[ARRAY_SIZE]; double highTemp[ARRAY_SIZE]; int LoadData (char com); void ShowAll (int count); int main() { char command; cout << "Welcome to David's Libary Database!" << endl << endl << "(L)oad File, (S)how All, (F)ind City, (Q)uit "; cin >> command; LoadData(command); return 0; } int LoadData(char com) { int index = 0; double tempLow, tempHi; string singleCity; if(com == 'l' || com == '1' || com == 'L') { cout << "Please provide a file path: "; cin >> fileLocation; newList.open(fileLocation.c_str()); if(!newList.is_open()) { cout << "Please provide a file path: "; cin >> fileLocation; newList.open(fileLocation.c_str()); if(!newList.is_open()) { return -1; } } while(!newList.eof()) { newList >> singleCity >> tempLow >> tempHi; city[index] = singleCity; lowTemp[index] = tempLow; highTemp[index] = tempHi; index++; } cout << index +1 << " record(s) found." << endl; cout << city[0]; newList.close(); newList.clear(std::ios_base::goodbit); } return 0; } #包括 #包括 #包括 使用名称空间std; 常量int数组_SIZE=1000; 字符串文件位置; ifstream新列表; 字符串城市[数组大小]; 双低温[阵列大小]; 双高温[阵列大小]; int LoadData(char-com); void ShowAll(整数计数); int main() { char命令; cout文件定位; 打开(fileLocation.c_str()); 如果(!newList.is_open()) { 返回-1; } } 而(!newList.eof()) { 新列表>>单一城市>>模板>>坦菲; 城市[指数]=单一城市; 低温[指数]=模板流; 高温[指数]=温度; 索引++; } cout

分段故障核心转储并行阵列 我对C++非常陌生,我只是想读一个文本文件,它每行都有一个字符串和两个十进制数。当我试图将它们读入数组时,我会遇到一个分段错误……我仍在学习数组,不明白为什么这对我的一生都不起作用。代码如下: #include <iostream> #include <fstream> #include <string> using namespace std; const int ARRAY_SIZE = 1000; string fileLocation; ifstream newList; string city[ARRAY_SIZE]; double lowTemp[ARRAY_SIZE]; double highTemp[ARRAY_SIZE]; int LoadData (char com); void ShowAll (int count); int main() { char command; cout << "Welcome to David's Libary Database!" << endl << endl << "(L)oad File, (S)how All, (F)ind City, (Q)uit "; cin >> command; LoadData(command); return 0; } int LoadData(char com) { int index = 0; double tempLow, tempHi; string singleCity; if(com == 'l' || com == '1' || com == 'L') { cout << "Please provide a file path: "; cin >> fileLocation; newList.open(fileLocation.c_str()); if(!newList.is_open()) { cout << "Please provide a file path: "; cin >> fileLocation; newList.open(fileLocation.c_str()); if(!newList.is_open()) { return -1; } } while(!newList.eof()) { newList >> singleCity >> tempLow >> tempHi; city[index] = singleCity; lowTemp[index] = tempLow; highTemp[index] = tempHi; index++; } cout << index +1 << " record(s) found." << endl; cout << city[0]; newList.close(); newList.clear(std::ios_base::goodbit); } return 0; } #包括 #包括 #包括 使用名称空间std; 常量int数组_SIZE=1000; 字符串文件位置; ifstream新列表; 字符串城市[数组大小]; 双低温[阵列大小]; 双高温[阵列大小]; int LoadData(char-com); void ShowAll(整数计数); int main() { char命令; cout文件定位; 打开(fileLocation.c_str()); 如果(!newList.is_open()) { 返回-1; } } 而(!newList.eof()) { 新列表>>单一城市>>模板>>坦菲; 城市[指数]=单一城市; 低温[指数]=模板流; 高温[指数]=温度; 索引++; } cout,c++,arrays,fstream,C++,Arrays,Fstream,首先,尝试使用g++-g编译,然后运行gdb获取堆栈跟踪,以便知道段故障发生的位置 在代码中: while(!newList.eof()) { newList >> singleCity >> tempLow >> tempHi; city[index] = singleCity; lowTemp[index] = tempLow; highTemp[index] = tempHi

首先,尝试使用
g++-g
编译,然后运行
gdb
获取堆栈跟踪,以便知道段故障发生的位置

在代码中:

    while(!newList.eof())
    {
        newList >> singleCity >> tempLow >> tempHi;
        city[index] = singleCity;
        lowTemp[index] = tempLow;
        highTemp[index] = tempHi;
        index++;
   }

如果索引大于数组大小,则会出现分段错误。

首先,尝试使用
g++-g
编译,然后运行
gdb
以获取堆栈跟踪,以便知道分段错误发生的位置

在代码中:

    while(!newList.eof())
    {
        newList >> singleCity >> tempLow >> tempHi;
        city[index] = singleCity;
        lowTemp[index] = tempLow;
        highTemp[index] = tempHi;
        index++;
   }

如果索引大于数组大小,则会出现分段错误。

首先,尝试使用
g++-g
编译,然后运行
gdb
以获取堆栈跟踪,以便知道分段错误发生的位置

在代码中:

    while(!newList.eof())
    {
        newList >> singleCity >> tempLow >> tempHi;
        city[index] = singleCity;
        lowTemp[index] = tempLow;
        highTemp[index] = tempHi;
        index++;
   }

如果索引大于数组大小,则会出现分段错误。

首先,尝试使用
g++-g
编译,然后运行
gdb
以获取堆栈跟踪,以便知道分段错误发生的位置

在代码中:

    while(!newList.eof())
    {
        newList >> singleCity >> tempLow >> tempHi;
        city[index] = singleCity;
        lowTemp[index] = tempLow;
        highTemp[index] = tempHi;
        index++;
   }
如果索引大于数组大小,则会出现分段错误。

不要在(!newList.eof())检查文件结尾时使用
。
您应该将其修改为

while(newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
前提是城市中的字符之间没有任何空格。您的输入文件应该是

NY 23 24
LA 13 18
如果您的输入文件为

New York 23 24
Los Angeles 13 18
您应该检查它是否超出
city[]
数组的范围。

不要在(!newList.eof())
检查文件结尾时使用
。
您应该将其修改为

while(newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
前提是城市中的字符之间没有任何空格。您的输入文件应该是

NY 23 24
LA 13 18
如果您的输入文件为

New York 23 24
Los Angeles 13 18
您应该检查它是否超出
city[]
数组的范围。

不要在(!newList.eof())
检查文件结尾时使用
。
您应该将其修改为

while(newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
前提是城市中的字符之间没有任何空格。您的输入文件应该是

NY 23 24
LA 13 18
如果您的输入文件为

New York 23 24
Los Angeles 13 18
您应该检查它是否超出
city[]
数组的范围。

不要在(!newList.eof())
检查文件结尾时使用
。
您应该将其修改为

while(newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
前提是城市中的字符之间没有任何空格。您的输入文件应该是

NY 23 24
LA 13 18
如果您的输入文件为

New York 23 24
Los Angeles 13 18

您应该检查它是否超出
city[]
数组的范围。

除非您的文件中恰好有1000多个条目,否则seg故障没有明显的原因

我想猜测一下,你出错的原因是你的阅读循环不正确。始终测试您的读取是否成功。若读取失败,但不是因为文件结束,那个么您的循环将永远继续。我想这可能就是本案中发生的情况。在任何情况下,
newfile.eof()
都不会做您明显认为它会做的事情,它会而不是测试您是否在文件末尾

下面是编写循环的正确方法,尝试一下,看看是否有什么不同

while (newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
查看此代码如何实际测试读取是否成功(通过将读取本身置于while条件)

另一个错误

cout << index +1 << " record(s) found." << endl;

cout没有明显的seg故障原因,除非您的文件中有1000多个条目

我想猜测一下,你出错的原因是你的阅读循环不正确。始终测试您的读取是否成功。若读取失败,但不是因为文件结束,那个么您的循环将永远继续。我想这可能就是本案中发生的情况。在任何情况下,
newfile.eof()
都不会做您明显认为它会做的事情,它会而不是测试您是否在文件末尾

下面是编写循环的正确方法,尝试一下,看看是否有什么不同

while (newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
查看此代码如何实际测试读取是否成功(通过将读取本身置于while条件)

另一个错误

cout << index +1 << " record(s) found." << endl;

cout没有明显的seg故障原因,除非您的文件中有1000多个条目

我想猜测一下,你出错的原因是你的阅读循环不正确。始终测试您的读取是否成功。若读取失败,但不是因为文件结束,那个么您的循环将永远继续。我想这可能就是本案中发生的情况。在任何情况下,
newfile.eof()
都不会做您明显认为它会做的事情,它会而不是测试您是否在文件末尾

下面是编写循环的正确方法,尝试一下,看看是否有什么不同

while (newList >> singleCity >> tempLow >> tempHi)
{
    city[index] = singleCity;
    lowTemp[index] = tempLow;
    highTemp[index] = tempHi;
    index++;
}
查看此代码如何实际测试读取是否成功(通过将读取本身置于while条件)

另一个错误

cout << index +1 << " record(s) found." << endl;

cout没有明显的seg故障原因,除非您的文件中有1000多个条目

我想猜测一下,你出错的原因是你的阅读循环