为什么我收到 std::bad_alloc 错误
Why am I getting an std::bad_alloc error
我在 运行 下面的代码时遇到问题。每次我设置 while 循环到达 .eof() 它 returns a std::bad_alloc
inFile.open(fileName, std::ios::in | std::ios::binary);
if (inFile.is_open())
{
while (!inFile.eof())
{
read(inFile, readIn);
vecMenu.push_back(readIn);
menu.push_back(readIn);
//count++;
}
std::cout << "File was loaded succesfully..." << std::endl;
inFile.close();
}
如果我设置了预定的迭代次数,它运行良好,但当我使用 EOF 函数时失败。这是读取函数的代码:
void read(std::fstream& file, std::string& str)
{
if (file.is_open())
{
unsigned len;
char *buf = nullptr;
file.read(reinterpret_cast<char *>(&len), sizeof(unsigned));
buf = new char[len + 1];
file.read(buf, len);
buf[len] = '[=12=]';
str = buf;
std::cout << "Test: " << str << std::endl;
delete[] buf;
}
else
{
std::cout << "File was not accessible" << std::endl;
}
}
非常感谢您提供的任何帮助。
注意:我没有提到 vecMenu 的类型是 std::vector
菜单类型为 std::list
我看到的主要问题是:
您正在使用 while (!inFile.eof())
结束循环。见 Why is iostream::eof inside a loop condition considered wrong?.
在使用读入的变量之前,您没有检查对 ifstream::read
的调用是否成功。
我建议:
正在将 read
的版本更改为 return 对 ifstream
的引用。它应该 return 作为输入的 ifstream
。这使得在循环条件中使用对 read
的调用成为可能。
在使用前检查对 ifstream::read
的调用是否成功。
将对 read
的调用放在 while
语句的条件中。
std::ifstream& read(std::fstream& file, std::string& str)
{
if (file.is_open())
{
unsigned len;
char *buf = nullptr;
if !(file.read(reinterpret_cast<char *>(&len), sizeof(unsigned)))
{
return file;
}
buf = new char[len + 1];
if ( !file.read(buf, len) )
{
delete [] buf;
return file;
}
buf[len] = '[=10=]';
str = buf;
std::cout << "Test: " << str << std::endl;
delete[] buf;
}
else
{
std::cout << "File was not accessible" << std::endl;
}
return file;
}
和
inFile.open(fileName, std::ios::in | std::ios::binary);
if (inFile.is_open())
{
std::cout << "File was loaded succesfully..." << std::endl;
while (read(inFile, readIn))
{
vecMenu.push_back(readIn);
menu.push_back(readIn);
//count++;
}
inFile.close();
}
我在 运行 下面的代码时遇到问题。每次我设置 while 循环到达 .eof() 它 returns a std::bad_alloc
inFile.open(fileName, std::ios::in | std::ios::binary);
if (inFile.is_open())
{
while (!inFile.eof())
{
read(inFile, readIn);
vecMenu.push_back(readIn);
menu.push_back(readIn);
//count++;
}
std::cout << "File was loaded succesfully..." << std::endl;
inFile.close();
}
如果我设置了预定的迭代次数,它运行良好,但当我使用 EOF 函数时失败。这是读取函数的代码:
void read(std::fstream& file, std::string& str)
{
if (file.is_open())
{
unsigned len;
char *buf = nullptr;
file.read(reinterpret_cast<char *>(&len), sizeof(unsigned));
buf = new char[len + 1];
file.read(buf, len);
buf[len] = '[=12=]';
str = buf;
std::cout << "Test: " << str << std::endl;
delete[] buf;
}
else
{
std::cout << "File was not accessible" << std::endl;
}
}
非常感谢您提供的任何帮助。 注意:我没有提到 vecMenu 的类型是 std::vector 菜单类型为 std::list
我看到的主要问题是:
您正在使用
while (!inFile.eof())
结束循环。见 Why is iostream::eof inside a loop condition considered wrong?.在使用读入的变量之前,您没有检查对
ifstream::read
的调用是否成功。
我建议:
正在将
read
的版本更改为 return 对ifstream
的引用。它应该 return 作为输入的ifstream
。这使得在循环条件中使用对read
的调用成为可能。在使用前检查对
ifstream::read
的调用是否成功。将对
read
的调用放在while
语句的条件中。
std::ifstream& read(std::fstream& file, std::string& str)
{
if (file.is_open())
{
unsigned len;
char *buf = nullptr;
if !(file.read(reinterpret_cast<char *>(&len), sizeof(unsigned)))
{
return file;
}
buf = new char[len + 1];
if ( !file.read(buf, len) )
{
delete [] buf;
return file;
}
buf[len] = '[=10=]';
str = buf;
std::cout << "Test: " << str << std::endl;
delete[] buf;
}
else
{
std::cout << "File was not accessible" << std::endl;
}
return file;
}
和
inFile.open(fileName, std::ios::in | std::ios::binary);
if (inFile.is_open())
{
std::cout << "File was loaded succesfully..." << std::endl;
while (read(inFile, readIn))
{
vecMenu.push_back(readIn);
menu.push_back(readIn);
//count++;
}
inFile.close();
}