Thread: FILE pointers as argument

  1. #1
    Registered User Bogdokhan's Avatar
    Join Date
    Jan 2016
    Location
    Belgium
    Posts
    8

    FILE pointers as argument

    Hello,
    I'm rather newbie in C programming.
    Passing FILE pointer to functions seems to give some problems sometimes. My way to do it is as follow :

    Code:
    void read_file(FILE **file_pointer);
    
    int main(void)
    {
        FILE *f_in = NULL;
    ...
        f_in = fopen(<file_name>,"r");
        read_file(&f_in);
    ...
    }
    
    void read_file(FILE **file_pointer)
    {
        char *in_line = (char*) calloc (1000,sizeof(char));
    ...
        fgets(in_line, 1000, *file_pointer);
    ...
    }
    The advantage of this way of doing is you can open, read and close your file in separated blocks of code.

    If I don't use &f_in as argument, I get a segment fault with fgets.
    This is my solution for it. Tell me if you think I'm doing something wrong.

    Good day to everyone

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The advantage of this way of doing is you can open, read and close your file in separated blocks of code.
    It's not a very good advantage then. You will end up forgetting to call fclose() and leaking a file handle, which might actually be worse since there is a limited number of those that can be open at one time.

    Also, you just don't need to do it this way. Passing in a FILE * here would work just as well. The way that stdio works with FILE is an example of an opaque pointer. All of the functions that use FILE * know about the definition of the underlying structure, so they update that. The actual value of the pointer only matters because that is where the structure, with all the important information, is stored; the only occasion you might need a FILE ** is if you were going to open a file somewhere and change an existing FILE *, but again there would be better ways to write one. You could return the FILE * instead.

  3. #3
    Registered User Bogdokhan's Avatar
    Join Date
    Jan 2016
    Location
    Belgium
    Posts
    8
    Ok, i'll try making it simpler. It was only a workaround to avoid fgets to report a "segment fault 11". Thanks four your answer.

  4. #4
    Registered User Bogdokhan's Avatar
    Join Date
    Jan 2016
    Location
    Belgium
    Posts
    8
    Quote Originally Posted by whiteflags View Post
    the only occasion you might need a FILE ** is if you were going to open a file somewhere and change an existing FILE *, but again there would be better ways to write one. You could return the FILE * instead.
    This is the answer to my problem ! fopen() changes the FILE pointer.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-04-2012, 11:01 PM
  2. array of pointers passed as argument to fgets
    By shruthi in forum C Programming
    Replies: 16
    Last Post: 09-07-2012, 11:12 PM
  3. file pointer as argument
    By 01rich01 in forum C Programming
    Replies: 10
    Last Post: 03-22-2010, 11:26 PM
  4. passing argument to a file
    By warney_out in forum C Programming
    Replies: 2
    Last Post: 09-04-2004, 01:00 PM
  5. file pointer as an argument?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-12-2001, 09:31 PM

Tags for this Thread