473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fopen problem

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.

I really don't want to get into installing c++ or something like that.
I currently using Gcc in CYGWIN to do the compiling.

Any suggestions anyone?

Pete

Nov 15 '05 #1
10 4721
[I accidentally hit "reply to author" the first time, I am reposting to
the group.]

pj***@netscape.com wrote:
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.


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

Nov 15 '05 #2
>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
fgets() returns a string with a trailing newline (unless your entry
is longer than the buffer supplied to fgets). Did you remove it
before passing it to fopen()? You should unless you really have
files with names containing newlines, which is pretty unusual.
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.
If fopen() returns NULL, it failed. This apparently happened in
your program.
Then fgets is used to read a line from the
supposedly open file. This causes a stack error exception and a
stackdump.
Passing NULL to fgets() invokes the wrath of undefined behavior.
You should check the value returned by fopen() for being equal to
NULL before attempting to use it. If it is equal to NULL, print
an error message and do not attempt using it.
I think fopen is where the problem is because it is supposed to return
a small positive integer.
fopen() returns a pointer, *NOT* a small positive integer. If it
returns NULL, it failed.
To me '0' is not a small positive integer.
Forget the small positive integer. NULL is not guaranteed to be
represented as 0xdeadbeef, even on 32-bit machines, and it is
sometimes represented as 0.
I really don't want to get into installing c++ or something like that.
I currently using Gcc in CYGWIN to do the compiling.

Any suggestions anyone?


When you print out the filename you got to check it, put it in
quotes:

printf("The file name is '%s'\n", filename);

When it prints:

The file name is 'foobar.txt
'

you'll know you forgot to remove the trailing newline.

Also, always check if the return value of fopen() is equal to NULL
before trying to use it.

Although fopen() is not guaranteed to never return a non-zero small
positive integer as a pointer, there's a good chance that if it
does something is seriously wrong.

Gordon L. Burditt
Nov 15 '05 #3
pj***@netscape.com writes:
[...]
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.


You're probably confusing fopen() with open().

<OT>
The open() is not defined by the C standard; it's part of POSIX.
open() returns a small positive integer, known as a "file descriptor",
or -1 on error.
</OT>

fopen() is a standard C function that returns a pointer value (of type
FILE*); the returned values is NULL on error. (Pointers are not
integers.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4


Thanks for the replies. Yep, per the print test there is a newline
hung on the end of the filename! So the next question is how to remove
the little bugger, since in general I don't know beforehand how long
the filename will be.

Pete

Nov 15 '05 #5
pj***@netscape.com wrote:
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.


Did you remember to remove the '\n' from the line with the filename?
Nov 15 '05 #6
pj***@netscape.com wrote:

Thanks for the replies. Yep, per the print test there is a newline
hung on the end of the filename! So the next question is how to remove
the little bugger, since in general I don't know beforehand how long
the filename will be.

Pete

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char fname[FILENAME_MAX+1], *nl;
FILE *fp;
printf("What is the filename? ");
fflush(stdout);
if (!fgets(fname, sizeof fname, stdio)) {
fprintf(stderr, "I couldn't read that for some reason.\n");
exit(EXIT_FAILURE);
}
if ((nl = strchr(fname,'\n'))) *nl = 0;
if (!(fp = fopen(fname,"r"))) {
fprintf(stderr, "\"%s\" could not be opened for reading.\n",
fname);
exit(EXIT_FAILURE);
}
fclose(fp);
return 0;
}
Nov 15 '05 #7
pj***@netscape.com a écrit :
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


Try that :

infile=fopen(filename,"r");

and if you are usig fgets() to get the name from the user, be sure that
you have removed the trailing '\n' if exists.

As long as infile is 0 (NULL) don't go further. The opening of the file
has failed.
Nov 15 '05 #8
pj***@netscape.com a écrit :
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


Try that :

infile=fopen(filename,"r");

and if you are using fgets() to get the name from the user, be sure that
you have removed the trailing '\n' if exists.

As long as infile is 0 (NULL) don't go further. The opening of the file
has failed.
Nov 15 '05 #9

In article <ln************@nuthaus.mib.org>, Keith Thompson <ks***@mib.org> writes:

<OT>
The open() is not defined by the C standard; it's part of POSIX.
open() returns a small positive integer, known as a "file descriptor",
or -1 on error.
</OT>


<OT TYPE="still">
POSIX / SUS open returns a small *nonnegative* integer on success. 0
is a valid descriptor value. (I'm amazed at how often I encounter
bugs in POSIX programs due to assuming 0 isn't a valid descriptor.)
</OT>

--
Michael Wojcik mi************@microfocus.com

Reversible CA's are -automorphisms- on shift spaces. It is a notorious
fact in symbolic dynamics that describing such things on a shift of finite
type are -fiendishly- difficult. -- Chris Hillman
Nov 15 '05 #10
Thanks to all who responded to my fopen problem. Yoy are nice people.

pete

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Banks | last post by:
I'm using the following code to check if a file exists on the server. If not it should do one thing but if it does exist it should do something else. This seems to be working except when the file...
11
by: Tesla | last post by:
hey guys, i use Fopen to get the contents from a web page, but I get this error... Warning: fopen(http://seb.sleepbot.com:8000/played.html): failed to open stream: HTTP request failed! ICY 400...
5
by: pieterprovoost | last post by:
Hi, Can anyone tell me why going to http://www.algaebase.org/DistributionResponse.lasso?currentMethod=distro&sk=20&-session=abv3:D5C1E5F111d2e05040XVy1A0A54A yields results, while the same page...
1
by: Cleverbum | last post by:
I'm trying to write a script which downloads information from a number of websites analyses it and shows some results. The problem I'm having is that some sites seem to work perfectly while others...
0
by: milan.topalov | last post by:
I'm having some strange problems using fopen() to fetch website content. It seems I can't open URL shorter then certain lenght. I can open this:...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
16
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.