Reputation: 31
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”.
I also get a warning from Xcode: "Assignment makes pointer from integer without a cast" at line 10 of my code (main.c):
#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?
Upvotes: 0
Views: 437
Reputation: 399813
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.
Specifically, fgetc()
returns int
, which is required in order to fit the EOF
value. 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. That would suck, so that's not how it works. :)
Upvotes: 1