c++ - fscanf 中的 Exc_bad_access 错误

标签 c++ objective-c c xcode5

我在 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/

相关文章:

c++ - 闰年程序似乎没有运行

c# - 将复杂的结构(带有内部结构数组)从 C# 传递到 C++

ios - 在 UItableviewCell 上设置的图像

c - 隐式else编译器优化

c++ - 在 Qt MinGW 编译器中处理 __event

c++ - 3D 模型文件并从中创建三角形网格

objective-c - 让 LLVM/Clang 16 位对齐

objective-c - 将颜色从 colorWithRGBA 转换为 colorWithWhite

java - 如何使用 JNI 将终端输出从 C 程序重定向到 System.out?

c - 关于C结构体中某种类型元素的问题