简体   繁体   English

程序收到信号:“ EXC_BAD_ACCESS”?

[英]Program received signal: “EXC_BAD_ACCESS”?

I'm making this little program that just reads contents of a file, but when I run it I get this error: Program received signal: “EXC_BAD_ACCESS”. 我正在制作一个只读取文件内容的小程序,但是在运行它时出现此错误:程序收到信号:“ EXC_BAD_ACCESS”。

I also get a warning from Xcode: "Assignment makes pointer from integer without a cast" at line 10 of my code (main.c): 我还从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;
}

This is the debugger output: 这是调试器的输出:

[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)

Is there a problem with the pointer, wrong function? 指针有问题吗?函数是否错误? I found something to track memory issues wich is called NSZombie, what is that and can I use it? 我发现了一个跟踪内存问题的东西,称为NSZombie,那是什么,我可以使用它吗?

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

Here's another error: 这是另一个错误:

char c;

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

You should really study the documentation for important functions like fgetc() before using them. 在使用fgetc()类的重要功能之前,您应该真正学习它们的文档

Specifically, fgetc() returns int , which is required in order to fit the EOF value. 具体来说, fgetc()返回int ,这是适合EOF值所必需的。 If it returned char , then there would have to be one character whose numerical value collided with EOF and that, thus, couldn't appear in a binary file. 如果返回char ,那么将必须有一个字符,其数值与EOF发生冲突,因此不能出现在二进制文件中。 That would suck, so that's not how it works. 那会很烂,所以这不是它的工作原理。 :) :)

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

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