![]() |
| | #1 |
| Registered User Join Date: Oct 2001
Posts: 37
| Passing data/pointers between functions #2 Thanks, Alan Passing data/pointers between functions I have just started working with functions. I am trying to clarify how to pass data between these functions without declaring global variables. I am also trying to discover how to pass file pointers between functions. IE: The code I attached below opnes 3 files. If I wanted to open these 3 files in a separte function (say will call it "open_files). In the stock_report function, I want to call the open files function. How do I pass the file pointer into the stock_report function so that I can use the open_files function? |
| TankCDR is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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 );
}
Quzah. |
| quzah is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passing from functions in classes to another function in another class? | Robert_Sitter | Windows Programming | 1 | 12-13-2005 07:46 AM |
| passing 2dimensional arrays to functions | owi_just | C Programming | 1 | 04-25-2005 08:08 AM |
| passing structures to functions | AmazingRando | C++ Programming | 5 | 09-05-2003 11:22 AM |
| Passing structures between functions | TankCDR | C++ Programming | 2 | 01-29-2002 10:54 AM |
| What is wrong with this? (Passing (pointers to functions) to functions) | Leeman_s | C++ Programming | 1 | 01-20-2002 11:50 AM |