Passing data/pointers between functions #2

This is a discussion on Passing data/pointers between functions #2 within the C Programming forums, part of the General Programming Boards category; Below is the major portion of the original post. Quzah responded to my questions but I still cannot make the ...

  1. #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 Attached Files

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  2. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 09:08 AM
  3. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 12:22 PM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

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