1. 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)

The fopen() function return a value with type FILE*, which is a pointer.

In your function file_open(), the return type is int, it should be
changed to FILE*, so that the function becomes:

FILE* file_open(char *name, int mode)
{
     return fopen(name, "r");
}

P> Does anyone have any suggestions as to why this:

P> int file_open(char *name, int mode)
P> {

P> //	return open(name, mode);

P>         return fopen(name, "r");
P> }

P> Would return the following when compiled:

P> warning: return makes integer from pointer without a cast


P> This is driving me absolutely mad! I'm attempting to adjust the source of a
P> program I didn't write. For whatever reason, the old open() call is returning
P> an invalid file descriptor (134758848 to be exact) so I am trying to change
P> it from open() to fopen(). The files passed through this routine are all read
P> only types, so there is no reason for mode to ever be anything but 0 ("r") so
P> I figured just change the ,mode) to a "r"

P> If anyone could think of any reason why I would get that message. I have no
P> idea why, but I keep coming up with it.

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

3. Re: C question (linux)

----- Original Message ----- 
From: "Matt Lewis" <matthewwalkerlewis at yahoo.com>
To: <EUforum at topica.com>
Subject: 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");
> 

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()?

-- 
Derek

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

4. Re: C question (linux)

I am going to throw my 2 cents in here.
I am with you Derek.

An example of A correct syntax (of many you could be used use)
  that I use when I am writing Import programs for Appgen:
(Note: there are many ways to skin this cat)


/* Somewhere in your Program  */

/* Lets allocate a buffer for 256 characters */
char a_buffer [256] ;

/* Now let us define what our INCOMING record will look like.
    THIS DOES NOT allocate space, but simple defines what a
    TYPE called foreign should look like.
*/
struct foreign {
     char CUSTOMERCODE       [8]     ;   /* 8 bytes of data */
     char DOCNUMBER          [7]     ;   /* 7 Bytes of data */
     char PONUMBER           [15]    ;   /* 15 Bytes of data */
     char SALESREP           [2]     ;   /* 2 bytes of data */
     char TERMSCODE                  ;   /* 1 byte of data */
     char NEWLINE                    ;   /* gotta have the EOL terminator */
};

/* Now let us define just how many characters
    that the structure of "foreign" occupies.
    We store that number in FBUFFER.
*/
# define FBUFFER (sizeof (struct foreign))

/* Allocated some space for stuff that looks like
    the TYPE STRUCTURE of foreign and we call this space
    infile. This DOES the actual allocation in memory.
*/
struct foreign infile ;

/* I like to use this method for "pointing". This is done
     for clarity's sake. All we are saying is:
     "fptr" is a POINTER to stuff in a structure TYPE
     called foreign. It points to nothing right now, all
     we are doing is defining this variable.
*/
struct foreign *fptr ;

/* stdio file descriptor for a file
    Now, we have define a pointer of type FILE to
    a variable called "a_file"
*/
FILE *a_file;

/* Assign to the FILE pointer, where our file is.
    If the value returned is zero, then
    "Houston, we have a problem" and we cannot
    open our file, so we bail out */
if (!(a_file = fopen("MYFILENAME", "r"))) {
         printf("This is Bogus, I cannot open this file: %s", "MYFILENAME") ;
         exit(1);
     }
/* **** BIG NOTE:
    we could have easily programmed this to look like:
    a_file = fopen("MYFILENAME", "r") ;
    if( a_file == 0 ) {
      printf("This is Bogus, I cannot open this file: %s", "MYFILENAME") ;
      exit(1);
     }
*/

/* Now everything is looking good, so we are going to assign
    a pointer that gives us the "address of" the area we
    allocated to the record we NOW call infile */
fptr = &infile ;

/* Ok, things get a little more complicated here:
    We are going to a while loop and read our input file.

    The fgets says:

    Read characters from "a_file" until either the
    size defined of FBUFFER - 1 or we get a "newline",
    whichever occurs first. The incoming character
    stream gets stored in the character array pointed to
    by "the address of" infile.

    (ED's Notes: I am a little rusty, is the following
     true statements/information ?)
    Since we are using the  "address of", which is an integer,
    and we DO NOT want C to complain, we "typecast"
    with the "(char *)" the address of infile, so that
    C will say "Oh yeah, the "address of" you are giving me
    is a pointer to a character array, so I am cool"
    THIS simple means we are telling C "(char *) &infile"
    is the buffer we want to put stuff in.

    Now fgets will also store the "NEWLINE" character it
    has read as well.
    */
while ( fgets( (char *) &infile, FBUFFER, a_file )) {

     /* Ok , we are going to "string print" into a_buffer,
        a string (%s) of "dot eight" (.8), which says only
        take 8 BYTES of data (otherwise we would ALL of the data
        until a NULL is encountered starting at our pointer)
        with the "pointer" fptr-> (Must have the "->") which
        is "pointing to" the area called "infile" and the
        LOCATION inside of "infile" called CUSTOMERCODE.
        (FWIW - Could as well been "PONUMBER" of any other
          names we defined in TYPE STRUCTURE called foreign).
        I do this so I can pass "a_buffer" to various functions
        that scrub (clean up) the data held in "a_buffer"
     sprintf( a_buffer, "%.8s\0", fptr->CUSTOMERCODE ) ;

      -BLAH - BLAH - BLAH Do some processing

} ; /* The end of our WHILE loop */


/* Close the file(s) */
fclose(a_file);

/* Be cute */

printf("Conversion is now complete...\n") ;
exit(0);

-------------------- End of Example --------------------

Now, I know this a LOT of information, I only hope it helps
you. I dunno what your C expertise is, and this IS NOT meant
to demean you or your abilities in anyway, but if helps you
or anyone else to understand a little bit more than they did,
then I have done my duty.

Thanks ...

Derek Parnell wrote:
> 
> 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()?
> 

-- 
Ben Duncan   Phone (601)-355-2574     Fax (601)-355-2573   Cell (601)-946-1220
                         Business Network Solutions
                      336 Elton Road  Jackson MS, 39212
    "Software is like Sex, it is better when it's free" - Linus Torvalds

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

Search



Quick Links

User menu

Not signed in.

Misc Menu