我在 iOS 项目的 Xcode 安装程序中运行以下代码,并在第二个 fscanf
命令上获取 EXC_BAD_ACCESS(代码=2,地址 0x30),即 fscanf(fid, "% d", &fIndex);
在第一次迭代中,即 j = 0
;
char word[wordLength]; // wordlength is set to 4
int numFiles = 0;
int fIndex = 0;
// create the word-fileIndex mapping
for(long i = 0; i < numWords; i++)
{
fscanf(fid, "%s%d", word, &numFiles);
vector<int> indexList(numFiles,0);
for(int j = 0; j < numFiles; j++)
{
fscanf(fid, "%d", &fIndex);
indexList[j] = fIndex;
}
wordIndexMap[word] = indexList;
}
但是,为了测试,我在第一个 fscanf
中添加了另一个要扫描的值,如下所示:
fscanf(fid, "%s%d%d", word, &numFiles, &fIndex);
它运行良好并从文件中读取了正确的值。
谁能告诉我这里发生了什么?
我的输入文件是这样的:
abcd num_Index index1 index2 ....
例如
1234 2 14 15
1235 3 5 2 6
1111 1 1
最佳答案
我认为您应该将 wordLength
设置为大于 4,因为您需要包括结尾的 '\0'
。在任何情况下,您在读取这样的输入时都需要小心并限制您正在读取的字符数。
关于c++ - fscanf 中的 Exc_bad_access 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686760/