C++ 分段故障(磁芯转储)读取线进入阵列
我试图将一个文件读入一个数组,以便将数组处理为选择排序。但是当我试图读取文件时,我得到了一个分段错误(内核转储)错误。这是我的密码:C++ 分段故障(磁芯转储)读取线进入阵列,c++,segmentation-fault,C++,Segmentation Fault,我试图将一个文件读入一个数组,以便将数组处理为选择排序。但是当我试图读取文件时,我得到了一个分段错误(内核转储)错误。这是我的密码: #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string array[40707]; int loop =
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string array[40707];
int loop = 0;
int loop2;
string line;
ifstream inFile("beowulf.txt");
if (inFile.is_open())
{
while(!inFile.eof())
{
getline(inFile, line);
array[loop] = line;
loop++;
}
inFile.close();
}
else cout << "Unable to open file" << endl;
for (loop2 =0; loop2 <= loop; loop2++)
cout << array[loop2] << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串数组[40707];
int循环=0;
int-loop2;
弦线;
ifstream-infle(“beowulf.txt”);
if(infle.is_open())
{
而(!infle.eof())
{
getline(填充,行);
数组[循环]=行;
loop++;
}
infle.close();
}
否则无法将字符串数组更改为:
std::vector<std::string> array;
然后,您的代码将更改为如下内容
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> array;
string line;
ifstream inFile("beowulf.txt");
if (!inFile.is_open()) {
cerr << "Unable to open file" << endl;
return 1;
}
while (getline(inFile, line))
array.push_back(line);
inFile.close();
for (int i = 0; i < array.size(); ++i)
cout << array[i] << endl;
return 0;
}
#包括
使用名称空间std;
int main(){
矢量阵列;
弦线;
ifstream-infle(“beowulf.txt”);
如果(!infle.is_open()){
cerr我可以马上看到两个潜在的错误案例
溢出数组结尾的结尾。读取时可能会发生溢出,因为没有保护循环。如果数组读取正好是40707行,则在loop2==循环时打印时会发生溢出。这两种情况都可能是SEGFULT的原因。推荐的解决方案是使用C++的std::vector,因为它会动态调整其EL的大小f调用输入并自动迭代存储的项
第二个错误不太严重,但允许在IO错误上进行无限循环。如果错误阻止读取行并使流处于错误状态,则可能永远无法到达文件结尾。getline不太可能发生,但格式化读取通常会发生
使用OP的大部分解决方案:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> array; // vector eliminates buffer overflow problem of string array.
//int loop = 0; vector also eliminates the need for this counter
//int loop2; and this one
string line;
ifstream inFile("beowulf.txt");
if (inFile.is_open())
{
while(getline(inFile, line)) //exits if anything goes wrong with the file IO
{
array.push_back(line); // stores line in vector
}
inFile.close();
}
else cout << "Unable to open file" << endl;
// C++11 way
for (string const & str: array)
{ // iterates through the string automatically
cout << str << endl;
}
/* Old C++ way
for (vector<string>::iterator str = array.begin();
token != array.end();
++token)
{ // iterates through the string manually
cout << *str << endl;
}
*/
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
vector数组;//vector消除了字符串数组的缓冲区溢出问题。
//int loop=0;向量也消除了对该计数器的需要
//int loop2;这个呢
弦线;
ifstream-infle(“beowulf.txt”);
if(infle.is_open())
{
while(getline(infle,line))//如果文件IO出现任何问题,则退出
{
array.push_back(line);//将行存储在向量中
}
infle.close();
}
否则不能尝试使用std::vector
而不是string
数组。然后,您可以使用array.push_back(line)
而不是索引。文件中有多少行?我看不到任何范围检查。请在(!infle.eof()&&loop<40707)时尝试
。但无论如何,这是一个糟糕的解决方案。我会使用向量或至少是动态可重新分配的数组。此外,如果循环
等于40707,则循环的打印读数超出了数组边界。而(!eof
模式是错误的,大多数情况下都是好技巧,但它逐字阅读,而不是逐行购买。@WhozCraig是的,我实际上是从问题中复制的。谢谢你指出。除非你也喜欢制作不必要的副本,“C++11方式”使用auto-const&str:array
@WhozCraig与您讨论const和reference会更好,但是auto
让编译器做任何可能超过string
清晰度的聪明事情吗?auto&&
绝对不会,但这完全是另一个主题。@WhozCraig明白了。将它作为strin在这种情况下,我想让发生的事情尽可能的明显。谢谢!现在我要转一转,看看当你设置一个通用引用时会发生什么。
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> array;
string line;
ifstream inFile("beowulf.txt");
if (!inFile.is_open()) {
cerr << "Unable to open file" << endl;
return 1;
}
while (getline(inFile, line))
array.push_back(line);
inFile.close();
for (int i = 0; i < array.size(); ++i)
cout << array[i] << endl;
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> array; // vector eliminates buffer overflow problem of string array.
//int loop = 0; vector also eliminates the need for this counter
//int loop2; and this one
string line;
ifstream inFile("beowulf.txt");
if (inFile.is_open())
{
while(getline(inFile, line)) //exits if anything goes wrong with the file IO
{
array.push_back(line); // stores line in vector
}
inFile.close();
}
else cout << "Unable to open file" << endl;
// C++11 way
for (string const & str: array)
{ // iterates through the string automatically
cout << str << endl;
}
/* Old C++ way
for (vector<string>::iterator str = array.begin();
token != array.end();
++token)
{ // iterates through the string manually
cout << *str << endl;
}
*/
return 0;
}