seeker
seeker

Reputation: 6991

Segmentation fault opening files

Im just trying to get started with learning C http://fromlearncodethehardway.com. However I'm stuck with a segmentation fault while trying to code up one of the examples from there. Here's the link to my source code. I tried using valgrind to further help with the debugging but I cant seem to figure out the issue. Here's the stack trace from valgrind.

   ~/Chardway$ valgrind ./ex17 db.dat g
   ==4423== Memcheck, a memory error detector                                                                                 == 4423== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
 ==4423== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
 ==4423== Command: ./ex17 db.dat g
 ==4423== 
 ==4423== Invalid read of size 1
 ==4423==    at 0x4EA4F1D: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:271)
 ==4423==    by 0x4E99DB5: __fopen_internal (iofopen.c:93)
 ==4423==    by 0x400A76: Database_open (ex17.c:65)
 ==4423==    by 0x400F7C: main (ex17.c:151)
 ==4423==  Address 0x722b is not stack'd, malloc'd or (recently) free'd
 ==4423== 
 ==4423== 
 ==4423== Process terminating with default action of signal 11 (SIGSEGV)
 ==4423==  Access not within mapped region at address 0x722B
 ==4423==    at 0x4EA4F1D: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:271)
 ==4423==    by 0x4E99DB5: __fopen_internal (iofopen.c:93)
 ==4423==    by 0x400A76: Database_open (ex17.c:65)
  ==4423==    by 0x400F7C: main (ex17.c:151)
.....

  ==4423== For counts of detected and suppressed errors, rerun with: -v
  ==4423== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 4 from 4)
 Segmentation fault

Any help on debugging would be great. Thanks!

Upvotes: 0

Views: 771

Answers (2)

lsiebert
lsiebert

Reputation: 667

Incidentally, I think you mean http://c.learncodethehardway.org/
The address you gave doesn't resolve.

Upvotes: 0

another.anon.coward
another.anon.coward

Reputation: 11395

In your code you have fopen calls as fopen(filename,'w'); & fopen(filename,'r+');. Those should be fopen(filename,"w"); & fopen(filename,"r+");. The second parameter to fopen should be const char *
Hope this helps!

Upvotes: 5

Related Questions