• C

FILE closing

for three decades on BCC and other C compiles I used to close files as follows:

Fabcd=(FILE *)fclose(Fabcd);

and prior to open a file to test if (!Fabcd) fopen(Fabcd, ...)

When using it with gcc I have "cast to pointer from integer to of different size (Wint-to-pointer-cast).

Any solution ?
versailleschercheur libreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
if (!Fabcd) fopen(Fabcd, ...)
means you can only call fopen(0, ...)
how can that work?
How is Fabcd declared?
0
phoffricCommented:
gcc compiler will be using these api's:

FILE * fopen ( const char * filename, const char * mode );
http://www.cplusplus.com/reference/cstdio/fopen/
"a null pointer is returned" if fails to open.

int fclose ( FILE * stream );
http://www.cplusplus.com/reference/cstdio/fclose/
"If the stream is successfully closed, a zero value is returned.
 On failure, EOF is returned."

Here is their fopen/fclose example. It doesn't look at all like your post since the first arg in fopen is:
const char * filename
/* fclose example */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","wt");
  fprintf (pFile, "fclose example");
  fclose (pFile);
  return 0;
}

Open in new window

I am not familiar with BCC. Could you post their APIs for fopen and fclose?
0
versailleschercheur libreAuthor Commented:
Sorry for the typo.

I certainly proceed as indicated, as follows:
char pfile    [100];  FILE *FpFile    =0L;         // documentation of the file.
....
sprintf(pfile   ,"%s\\...\\..."        , ...);             // ....

pFile = fopen (pfile, "r");

....

if (pFile) pFile=(FILE *) fclose (pFile);

Open in new window

It works well with BCC, but gcc dislike it.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

ozoCommented:
if( pFile && !fclose (pFile) ){ pFile=NULL; }
0
Subrat (C++ windows/Linux)Software EngineerCommented:
>> if (pFile) pFile=(FILE *) fclose (pFile);
here pFile is const char* but you are type casting to FILE* which is not correct.

More over, in fclose() you have to use FILE* but you are using char*
Ex:
fclose(FpFile); // or int retVal = fclose(FpFile); if interested in the status of close operation
0
ozoCommented:
It may be another typo, but http:#a40511980 uses pFile, which, unlike pfile and FpFile, is not declared, but the usage only makes sense if it was FILE *pFile;
0
phoffricCommented:
Your original OP suggests that you wanted to do a test to see whether you needed to open a file, which seemed reasonable enough. Your last post only tests to see whether you want to close a file, which is also reasonable.

Why do you want to take the int value returned from fclose, and force it to be a FILE* type? How are you then using the value of FpFile from the fclose?

Not sure if that really matters though, since you are asking about a warning message dealing with a  warning:
   cast to pointer from integer to of different size (Wint-to-pointer-cast).

This warning has two parts:
1. you start with an int (from fclose) and converting it to a pointer without a cast.
2. there is a "different size" issue, which also requires a cast.

You need two casts, one for each part of the problem. The following is platform dependent as sizes of int may vary.
FpFile= (FILE*) (long)fclose (FpFile);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoffricCommented:
If you just want to suppress that warning (which I don't recommend), then you can use the suppression flag to your gcc command: -Wno-int-to-pointer-cast
0
ozoCommented:
Even if you get the compiler to accept it, casting an EOF into a FILE* when the close fails should not be expected to produce anything sensible.
The only int value that can safely be cast into a FILE* is 0 (or a large enough integer type that was created from a cast of a valid FILE*)
0
phoffricCommented:
I agree with ozo that your code should make sense. That's why I was asking these questions above and was hoping to find a more meaningful approach:
Why do you want to take the int value returned from fclose, and force it to be a FILE* type? How are you then using the value of FpFile from the fclose?
There are times, however, where the wrong approach is needed for the short-term. If the author's code base has thousands of cases like this and other similar bad code that the BCC compiler accepts, then if the program functionality remains the same when bypassing the compiler warnings, that gives the developers time to make the correct adjustments and remove the warning suppression.
0
versailleschercheur libreAuthor Commented:
Sorry for the delay. This and your other comments give me some things to think about. Actually what I would need is a real refurbishing of the file functions as nI also use for some the MD5 in a parallel table to differentiate the need for update (I produce local sites to be duplicated globally).

Thank you !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.