Code:
#include <stdio.h>
#include <stdlib.h>
FILE *myfopen( const char *c )
{
/**
*** This code is purposefuly lengthy, to show
*** that you can indeed return a file pointer
*** from another function.
**/
FILE *fp;
fp = fopen( c, "r" );
return fp;
}
char *myfread( FILE *fp )
{
/**
*** Likewise.
**/
char buf[1024]={0};
char *b;
int len;
fread( buf, 1024, 1, fp );
len = strlen( buf );
b = malloc( len +1 );
b[len] = '\0';
strncpy( b, buf, len );
return b;
}
int main ( void )
{
FILE *fp = NULL;
char *s;
fp = myfopen( "myfile.txt" );
s = myfread( fp );
return puts( s );
}
That should be a decent example. (Assuming it works, I typed it while talking on the phone.)
Quzah.