Solved

FILE closing

Posted on 2014-12-21
11
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 84

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
Industry Leaders: 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 84

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 84

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 500 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 84

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

617 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