Thread: FILE * refrig (file pointer) to function

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    63

    FILE * refrig (file pointer) to function

    Hey all, thanks for all continuous help. Was wondering, a way to do this without declaring the FILE *refig in main (passing a pointer between functions). Thanks!

    Code:
    #include <stdio.h>
    
    void refrigerator_func(void);
    
    int main( void )
    {
    	refrigerator_func();
    }
    
    void refrigerator_func(void)
    {
    	FILE *refrig;
    
    	// ...
    	
    	fclose(refrig);
    }
    
    void add_entry(void)
    {
    	// ..
    	
    	fputs(fride_string, refrig);
    	fputs("\n");
    }

    P.S., it will be 1 year next month since I started C.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    What you want is a pointer to a pointer...
    Code:
    void add_entry(FILE **fileToAppend)
    {
        ....
    }
    ... and call it from refrigerator_func
    Fact - Beethoven wrote his first symphony in C

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,271
    You don't need a pointer to a pointer unless for some very special reason you need to modify the FILE pointer itself. A FILE* parameter will do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Laserlight is right - You would want to retain a specific information (like the position you are reading from) you would use a **, otherwise FILE* is fine.

    And because you are opening, adding something, and then closing it again, it doesn't matter
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a file pointer to a function
    By estefany in forum C Programming
    Replies: 4
    Last Post: 08-02-2012, 08:03 PM
  2. How to pass a file pointer to the function?
    By fredsilvester93 in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2012, 10:49 AM
  3. Moving a file pointer in a function
    By 127.0.0.1 in forum C Programming
    Replies: 27
    Last Post: 05-04-2011, 06:26 PM
  4. Have function return file pointer
    By krogz in forum C Programming
    Replies: 6
    Last Post: 05-03-2007, 08:56 PM
  5. Pointer problem with FILE *f in function
    By mabuhay in forum C Programming
    Replies: 6
    Last Post: 02-14-2006, 04:15 PM

Tags for this Thread