내 함수의 출력이 일치하지 않습니다. 이 함수는 파일을 열고 파일에서 정수를 추출한 다음 정수를 배열로 설정한다고 가정합니다. 20 개의 정수가 있으면 파일에서 배열로 추출하는 데 문제가 있습니다. 내가 이것을하려고 할 때, 나는 "배열이 범위 밖에있다"고보고있다.IO 파일 코드가 일치하지 않습니다
이 함수는 파일 이름이 잘못되었거나 파일의 컨텍스트에 정수가없는 경우에도 프롬프트를 표시한다고 가정합니다. 이 두 가지 모두 제대로 작동하는 것 같습니다.
도움을 주시면 감사하겠습니다.
bool loadArrayFromFile(int a[], int &n)
{
ifstream infile;
string fileName;
cout<<"Enter the name of file: ";
cin>>fileName;
infile.open(fileName.c_str());
if(!infile)
{
cout<<"File didn't open"<<endl; //if file name is incorrect or could not be opened
return false;
}
int count=0; //count values in file
int elem=0; //keeps track of elements
infile>>a[elem];
while(infile.good())
{
elem++;
count++;
infile>>a[elem];
}
if(!infile.eof())
{
cout<<"Wrong datatype in file"<<endl;
infile.clear();
infile.close();
return false;
}
n=count;
infile.close();
return true;
}
프로그램에서 코드를 보면 자동으로 벡터화됩니다. 그래서 그것이 제가 벡터화 한 이유입니다. – user1188766