Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FILE closing

Posted on 2014-12-21
11
Medium Priority
?
86 Views
Last Modified: 2015-03-18
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 ?
0
Comment
Question by:versailles
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 40511697
if (!Fabcd) fopen(Fabcd, ...)
means you can only call fopen(0, ...)
how can that work?
How is Fabcd declared?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40511889
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
 

Author Comment

by:versailles
ID: 40511980
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 40511992
if( pFile && !fclose (pFile) ){ pFile=NULL; }
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 40512009
>> 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
 
LVL 85

Expert Comment

by:ozo
ID: 40512020
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
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 40512062
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40512080
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
 
LVL 85

Expert Comment

by:ozo
ID: 40512147
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
 
LVL 32

Expert Comment

by:phoffric
ID: 40512154
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
 

Author Closing Comment

by:versailles
ID: 40673784
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question