을 얻고있다. 나는 .eof에 도달하는 동안 루프를 설정할 때마다() 그것이 표준 :: bad_alloc 뿐이다을 반환이유는 아래의 코드를 실행할 때이 문제에 봉착 성병 :: 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] = '\0';
str = buf;
std::cout << "Test: " << str << std::endl;
delete[] buf;
}
else
{
std::cout << "File was not accessible" << std::endl;
}
}
제공 할 수있는 도움이 있으면 감사하겠습니다. 참고 : 나는 vecMenu 유형의 표준 인 것이 : 벡터 언급하는 데 실패하고 메뉴 형 표준이다 :: 목록
이 게시물을 참조하십시오 : [? 왜 iostream :: EOF 잘못된 생각 루프 조건 내부 (http://stackoverflow.com/questions/ 5605125/왜-IS-iostreameof - 내 - 어 - 루프 조건으로 간주-잘못) – Rakete1111
당신이 읽을 프로그램을 늦추고 때마다 한 줄의 할당을 호출, 또한 Rakete1111 – Akatosh
@ 주셔서 감사합니다. 비 로컬'std :: vector'를 사용하고 매번'new/delete' 호출을하는 것보다'resize()'호출을하는 것이 더 좋습니다. – PaulMcKenzie