• 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?
 
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
 
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
 
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:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.