C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-02-2001, 08:29 PM   #1
Registered User
 
Join Date: Oct 2001
Posts: 37
Passing data/pointers between functions #2

Below is the major portion of the original post. Quzah responded to my questions but I still cannot make the program work. I have modified it (attached) but it now has 20+ errors and 40+ warnings. I know how to open files. My biggest problem is that I want to open the file in a separate function and then call it from other functions but cannot determine how to pass the pointer. If anyone can help, please view the attached program and pass comments.

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?
Attached Files
File Type: c program_2b.c (9.1 KB, 12 views)
TankCDR is offline   Reply With Quote
Old 11-02-2001, 09:49 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
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 );
}
That should be a decent example. (Assuming it works, I typed it while talking on the phone.)

Quzah.
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22