Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem fopen a FILE * from a function

Status
Not open for further replies.

netanel

Programmer
Oct 11, 2002
1
FR
Hello,
I have created a function fopen of a FILE *
and build a name.o

name.o :
void Fouverture (char *nomfic, char *typaction, FILE *fh)
{
*FILE *fh;
int rc=0;
fh=fopen(nomfic, typaction);
if (fh == NULL) { rc=1;}

return(rc);
}

Now I am trying to call this function from another
file name1.c, I want to call this function more then one time to open n FILE, and the problem is that when the function returns the FILE it is always NULL.

name1.c :

#include <stdio.h>
#define FICHIERA &quot;txt.txt&quot;
#define MAX 256



void main()
{
char mode_opt[2];
int ret;
FILE *fh;

strcpy(mode_opt,&quot;r&quot;);

Fouverture(FICHIERB, mode_opt, &fh);
if (fh == NULL)
{
printf(&quot;imposible to open file to

read:<txt1.txt>\n&quot;);
exit(1);
}

Ffermeture(fh);
}
 
You redeclare the file pointer in the function Fouverture. Does not the compiler gives you any warning?

Also, make sure that the file you open is in the directory where the program resides. Or, give the full path as parameter to the function.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
You also define FISCHIERA as &quot;txt.txt&quot;
but you pass FISCHIERB to Fouverture
HTH
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
why not simply

main()
{
FILE *fd = (FILE *)NULL;
FILE *openthatfile(FILE *, char *, char *);

if(!(fd = openthatfile(fd,filename,mode))) exit(1)

work on fd ....

fclose(fd);
exit(0);
}
FILE *openthatfile(FILE *fd,char *name,char *mode)
{
make checks you need
using stat or lstat
permissions, type....
exemple: if is a dir return((FILE *)NULL);

if((fd = fopen(name,mode))) return(fd);
return((FILE *)NULL);
} vox clamantis in deserto.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top