Re: C question (linux)
- Posted by Ben Duncan <bns at meta3.net> Dec 11, 2003
- 505 views
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