myOpenFile function

K

kak3012

Hi;

Here is the main code and the function. I think it does not return the (char
pIn)


I might be mistaken a lot of place like returning a value, choosing right
function type and so on. That is why I could not figure it by myself.

Can anyone help me I am new to C++ functions.


MAIN.CPP
------------------------
FILE Open_File(char *FILEStatic,char *OpenStyle,char *INFO);
int main(int argc, char *argv[])
{
// I get the file name from the command line
// And open it with my function
// I will use the pIn value to read the data with fread. But it returns NULL


pIn=Open_File(FILEin,"rb","INPUT");
}

MYFUCNTION
------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char Buffer[1024];
extern "C"
{
FILE Open_File(char *FILEStatic,char *OpenStyle,char *INFO);
}
FILE Open_File(char *FILEStatic,char *OpenStyle,char *INFO)
{
FILE *pStatic;
pStatic = fopen(FILEStatic, OpenStyle);
if (pStatic == NULL)
{
sprintf(Buffer, " ERROR: %s=%s\n", INFO,FILEStatic);
printf(Buffer);
// return (2);
}
else
{
sprintf(Buffer, " OK: %s=%s\n", INFO,FILEStatic);
printf(Buffer);
}
return *pStatic;
}
 
D

David Harmon

On Sat, 22 Jan 2005 11:42:58 +0100 in comp.lang.c++, "kak3012"
pStatic = fopen(FILEStatic, OpenStyle);
if (pStatic == NULL)
{
sprintf(Buffer, " ERROR: %s=%s\n", INFO,FILEStatic);
printf(Buffer);

That fails to print the error code giving the reason why the open
failed. Instead of printf(Buffer), should be
perror(Buffer);
 
J

Jack Klein

Hi;

Here is the main code and the function. I think it does not return the (char
pIn)


I might be mistaken a lot of place like returning a value, choosing right
function type and so on. That is why I could not figure it by myself.

Can anyone help me I am new to C++ functions.


MAIN.CPP

Here you declare a function with C++ linkage that returns a FILE
object, not a pointer to a FILE object as all C library functions
accept or return.
int main(int argc, char *argv[])
{
// I get the file name from the command line
// And open it with my function
// I will use the pIn value to read the data with fread. But it returns NULL

You haven't shown us the definitions of either pIn or FILEin. We have
no way of knowing how you go about getting a file name from the
command line, or if you do it correctly.
pIn=Open_File(FILEin,"rb","INPUT");

What type is pIn? Is it a FILE object? It is a pointer to a FILE
object?
}

MYFUCNTION
------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char Buffer[1024];
extern "C"
{
FILE Open_File(char *FILEStatic,char *OpenStyle,char *INFO);

Here you define a function with C linkage, but your other source file
tries to call a function of the same name with C++ linkage. If this
program links, your compiler and tool set is broken.
}
FILE Open_File(char *FILEStatic,char *OpenStyle,char *INFO)
{
FILE *pStatic;
pStatic = fopen(FILEStatic, OpenStyle);
if (pStatic == NULL)
{
sprintf(Buffer, " ERROR: %s=%s\n", INFO,FILEStatic);
printf(Buffer);
// return (2);
}
else
{
sprintf(Buffer, " OK: %s=%s\n", INFO,FILEStatic);
printf(Buffer);
}
return *pStatic;

You are actually trying to return a copy of a FILE object. That's a
VERY BAD IDEA. Why do you think that you want to do that?

Since operations on a copy of a file operation are not required to
work, using it causes undefined behavior.

Specifically, the ISO C standard, from which C++ inherits <cstdio>
functionality, specifically states:

"The address of the FILE object used to control a stream may be
significant; a copy of a FILE object may not necessarily serve in
place of the original."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top