1. RE: C question (linux)

fopen returns a FILE *. The return type should be FILE *, and any time you 
used int for a file descriptor, you have to replace it with FILE *.

>From: Paul <draegur at comcast.net>
>Subject: C question (linux)
>
>Does anyone have any suggestions as to why this:
>
>int file_open(char *name, int mode)
>{
>
>//	return open(name, mode);
>
>         return fopen(name, "r");
>}
>
>Would return the following when compiled:
>
>warning: return makes integer from pointer without a cast
>
>
>This is driving me absolutely mad! I'm attempting to adjust the source of a
>program I didn't write. For whatever reason, the old open() call is 
>returning
>an invalid file descriptor (134758848 to be exact) so I am trying to change
>it from open() to fopen(). The files passed through this routine are all 
>read
>only types, so there is no reason for mode to ever be anything but 0 ("r") 
>so
>I figured just change the ,mode) to a "r"
>
>If anyone could think of any reason why I would get that message. I have no
>idea why, but I keep coming up with it.
>
>Thanks!
>

new topic     » topic index » view message » categorize

2. RE: C question (linux)

Hi
fopen returns a pointer to the file, and the function is defined as int.

Sloppy programming I know, but I rarely define function types, (unless 
I'm not returning ints) (hey, it's my style, and I don't have a degree 
in programming!) so I'm not sure if this will work, but try

FILE *file_open(char *name, int mode) // shouldnt mode be char * to "r", 
"rw" etc?
{FILE *fp;
        fp = fopen(name, "r");

        return (fp);

        //altenatively copy fp to long int and return that

}
        
Chris


Paul wrote:
> 
> 
> Does anyone have any suggestions as to why this:
> 
> int file_open(char *name, int mode)
> {
> 
> //	return open(name, mode);
> 
>         return fopen(name, "r");
> }
> 
> Would return the following when compiled:
> 
> warning: return makes integer from pointer without a cast
> 
> 
> This is driving me absolutely mad! I'm attempting to adjust the source 
> of a 
> program I didn't write. For whatever reason, the old open() call is 
> returning 
> an invalid file descriptor (134758848 to be exact) so I am trying to 
> change 
> it from open() to fopen(). The files passed through this routine are all 
> read 
> only types, so there is no reason for mode to ever be anything but 0 
> ("r") so 
> I figured just change the ,mode) to a "r"
> 
> If anyone could think of any reason why I would get that message. I have 
> no 
> idea why, but I keep coming up with it.
> 
> Thanks!
>

new topic     » goto parent     » topic index » view message » categorize

3. RE: C question (linux)

Paul wrote:
> 
> int file_open(char *name, int mode)
> {
> 
> //	return open(name, mode);
> 
>         return fopen(name, "r");
> }
> 
> Would return the following when compiled:
> 
> warning: return makes integer from pointer without a cast
> 
> 
> This is driving me absolutely mad! I'm attempting to adjust the 
> source of a program I didn't write. For whatever reason, the old 
> open() call is returning an invalid file descriptor (134758848 to 
> be exact) so I am trying to change it from open() to fopen(). The 
> files passed through this routine are all read only types, so there 
> is no reason for mode to ever be anything but 0 ("r") so I figured 
> just change the ,mode) to a "r" 

Rather than changing the return type of your function (as many have 
suggested), why not just cast to an integer?  It you change the return 
type, you'll only generate lots of other errors wherever the function is 
called, because the calls will all expect an integer.  So change 

return fopen(name, "r");

to

return (int) fopen(name, "r");

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

4. RE: C question (linux)

Derek Parnell wrote:
> 
> 
> From: "Matt Lewis"
> > 
> > 
> > Paul wrote:
> > > 
> > > int file_open(char *name, int mode)
> > > {
> > > 
> > > // return open(name, mode);
> > > 
> > >         return fopen(name, "r");
> > > }
> > 
> > Rather than changing the return type of your function (as many 
> > have suggested), why not just cast to an integer?  It you change 
> > the return type, you'll only generate lots of other errors 
> > wherever the function is called, because the calls will all 
> > expect an integer.  So change 
> > 
> > return fopen(name, "r");
> > 
> > to
> > 
> > return (int) fopen(name, "r");
> > 
> 
> I'm sorry Matt. I just have to respond to this one.
> 
> !!!DO NOT DO WHAT MATT HAS SUGGESTED!!!
> 
> It will only bring you bugs and more bugs. An integer is not a 
> pointer. Forcing C to convert (read: 'change') a pointer into an 
> int, just to stop warning messages is plain stupid (IMNSHO). If you 
> are going to do something, at least do a good job of it. 
> 
> Why do you want your code to return an int? Or is it just that you 
> wish to use fopen()?

Yep, that's what I get for trying to talk about C in a Euphoria forum. 
:)  You've uncovered my C coding technique: trial and (lot's of) error.  
And type casting has always bothered me since you haven't changed or 
converted anything besides the compiler's idea of what the thing is.

Having now actually read up on the difference between open and fopen, 
clearly my solution would ultimately fail.  But, then again, so will the 
other solutions, since the two methods use completely different ways to 
read a file.

Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu