我试图通过从文件中获取行将文本文件存储到多维数组中,将它们存储在变量中,然后将这些变量添加到数组中。
string QUESTIONS[3][49];
int x;
ifstream myFile;
myFile.open("Questions.txt");
getline(myFile, tempNum);
questionNum = stoi(tempNum);
for (int x = 0; x == 49; x++)
{
getline(myFile, line1);
getline(myFile, line2);
getline(myFile, line3);
getline(myFile, line4);
getline(myFile, tempNum);
correctAns = stoi(tempNum);
QUESTIONS[x][0] = line1;
QUESTIONS[x][1] = line2;
QUESTIONS[x][2] = line3;
QUESTIONS[x][3] = line4;
x++;
}
Ass1.exe中0x0FB231CA(msvcr120d.dll)的未处理异常:0xC0000005:访问冲突写入位置0xCCCCCCCC。
非常感谢任何帮助。
答案 0 :(得分:3)
我认为解决办法可能是:
string QUESTIONS[50][4];
int x;
ifstream myFile;
myFile.open("Questions.txt");
getline(myFile, tempNum);
questionNum = stoi(tempNum);
for (int x = 0; x < 50; x++)
{
getline(myFile, line1);
getline(myFile, line2);
getline(myFile, line3);
getline(myFile, line4);
getline(myFile, tempNum);
correctAns = stoi(tempNum);
QUESTIONS[x][0] = line1;
QUESTIONS[x][1] = line2;
QUESTIONS[x][2] = line3;
QUESTIONS[x][3] = line4;
}
你有两次增加x,并且字符串数组没有以正确的顺序声明。