[I accidentally hit "reply to author" the first time, I am reposting to
the group.]
pjlsr@netscape.com wrote:[color=blue]
> It's close to twenty years since I used the C language and at that time
> I was doing only floating point computational work, nothing with
> strings or reading files. I tried to use fopen in the following manner.
> a file name is entered by keyboard , fgets is used to read the name.
> printf is used to confirm that the name was correctly read. Then
> infile=fopen("filename","r") is used to open the file which very
> definitly exists. It returns '0' as the ptr( I think that is the term),
> which makes me suspicious. Then fgets is used to read a line from the
> supposedly open file. This causes a stack error exception and a
> stackdump.
>
> I think fopen is where the problem is because it is supposed to return
> a small positive integer. To me '0' is not a small positive integer.[/color]
fopen returns a FILE pointer on success, NULL (0) on error, your fopen
call is obviously failing. You then call fgets with the NULL pointer
returned from fopen which invokes undefined behavior, the effects of
which may include a "stack error exception", whatever that is.
You said you were reading the filename with fgets, fgets will store the
newline in the buffer if there is room, does the name of the file you
are trying to open end with a newline? Are you removing the newline
from the buffer before passing it to fopen? If you have any more
questions, please post a small but complete and compilable example that
produces the undesired behavior.
Robert Gamble