[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: problems with gcc and fopen() in debian 2.2



On Wed, Aug 08, 2001 at 09:16:52AM +0200, Andreas Maresch wrote:
> Hello!
> 
> I am (re)starting in programming c (I have learned it a while ago
> under dos).  Yesterday, I wrote a simple program.  When compiled with
> gcc -o test test.c, no errors or warnings occured.
> 
> But when I try to run, it terminates with "Segmentation fault".  So,
> with the help of ddd (real nice tool ;-D ) I was able to locate the
> problem: fopen().
> 
> ddd reported a seg fault on the following line:
> 
> infile=fopen("dir.dat", "r");
> 
> The "dir.dat" exists (in the same directory), and infile was deklared
> with: FILE *infile;

My crystal ball says you have a bounds violation in an assignment
earlier in the program, but it does not manifest itself as as Segment
Violation until fopen() attempts to allocate a FILE structure.

Remember, fopen() fails by returning a NULL pointer if it can't open the
file -- not by crashing your program (so check the return value!).

-- 
Eric G. Miller <egm2@jps.net>



Reply to: