繁体   English   中英

程序收到信号:“ EXC_BAD_ACCESS”?

[英]Program received signal: “EXC_BAD_ACCESS”?

我正在制作一个只读取文件内容的小程序,但是在运行它时出现此错误:程序收到信号:“ EXC_BAD_ACCESS”。

我还从Xcode收到警告:在代码(main.c)的第10行中,“赋值使指针从整数开始而没有强制转换”:

#include <stdio.h>
#include <stdlib.h>

#define FILE_LOCATION "../../Data"

int main (int argc, const char * argv[]) {
    FILE *dataFile;
    char c;

    if ( dataFile = fopen(FILE_LOCATION, "r") == NULL ) {
        printf("FAILURE!");
        exit(1);
    }

    while ( (c = fgetc(dataFile)) != EOF ) {
        printf("%c", c);
    }

    fclose(dataFile);

    return 0;
}

这是调试器的输出:

[Session started at 2012-06-27 10:28:13 +0200.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1515) (Sat Jan 15 08:33:48 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 34331]
Running…
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)

指针有问题吗?函数是否错误? 我发现了一个跟踪内存问题的东西,称为NSZombie,那是什么,我可以使用它吗?

if ( (dataFile = fopen(FILE_LOCATION, "r")) == NULL ) {

这是另一个错误:

char c;

while ( (c = fgetc(dataFile)) != EOF ) {

在使用fgetc()类的重要功能之前,您应该真正学习它们的文档

具体来说, fgetc()返回int ,这是适合EOF值所必需的。 如果返回char ,那么将必须有一个字符,其数值与EOF发生冲突,因此不能出现在二进制文件中。 那会很烂,所以这不是它的工作原理。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM