Pointer problem with FILE *f in function

This is a discussion on Pointer problem with FILE *f in function within the C Programming forums, part of the General Programming Boards category; I declared Code: FILE *f = NULL; in the main-function. Then I call a function, where I close the file ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    Pointer problem with FILE *f in function

    I declared
    Code:
    FILE *f = NULL;
    in the main-function. Then I call a function, where I close the file f and open it again. I thought, the function prototype can look like this:
    Code:
    void my_function(FILE *f);
    and I call it in the main-function this way:
    Code:
    my_function(f);
    But now, in my_function, when I close the file and open it again, the pointer is messed up in the main-function.


    When I have the prototype this way:
    Code:
    void my_function(FILE **f);
    and call the function this way:
    Code:
    my_function(&f);
    then it works, but it doesn't looks that good with the pointer to the pointer...

    Any suggestions how to remove the double-pointer?

    thx

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could define it like this:
    Code:
    FILE *my_function(FILE *f)
    {
    .
    .
       return f;
    }
    And then call it like this:
    Code:
    f = my_function(f);
    Last edited by swoopy; 02-14-2006 at 09:53 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    hmmyes, but I have to return error numbers as int... So the easiest way would be the FILE **f. Or is there another solution?

    thx @swoopy

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Rather than a close and reopen, why not do an open and a close? And then pass the name of the file as a parameter? Open the file, do your thing, close the file, return success/failure?

    [edit]With your other posts, I'm wondering if this might not be similar to what you may be trying to do.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    I'm not programming for a PC, I'm programming with TIGC-C for my calculator. So there's a way to add a user file extension to the file or to archive the file, and for that I have to close the file first. To make the function easier to use, I don't think it's good if you have to open your file yourselve after each call of the function.
    It would also be faster, if you call the function several times in a loop to load more than one block from the file, when the function does not open and close the file each time (there are options you can control if you want to add a file extension, archive it, ...)

    [edit]With your other posts, I'm wondering if this might not be similar to what you may be trying to do.
    Yes, kind of. But I added some options to my function that you can read, add, edit, ... data.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Because fopen modifies fp
    Code:
    fp = fopen(fn, mode);
    you need a ** or need to return fp. (Or you could use a reference in C++.) I would suggest a **. If you really don't like how it looks you can use a typedef.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    I'm now using a **. It's the easiest way I think.

    Thx for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 01:54 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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