Thread: File pointers as function parameters

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    File pointers as function parameters

    Hey,

    I'm trying to pass some file pointers as function parameters but I'm having a little trouble with it.

    Say, I pass the pointer to a function to open my file:

    openF(&FilePntr, argv);


    ...


    OpenF(FilePntr, argv)
    FILE FilePntr;
    argv[];
    {
    *FilePntr = fopen(argv[1], "r");
    }


    Assuming the function's as simple as this, this is pretty much what I have done.

    The compiler generates an error and says that on line 5 there's a type mis match.

    How can I get around this? It's important that the program alters the variables used in the other functions by passing pointers as parameter functions, but how can I do it?

    Thanks alot,

    Lewis

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well it appears you are using a very old style of declaring parameters. Try this:
    Code:
    void OpenF(FILE **ptr_to_Fileptr, const char **argv)
    {
      *ptr_to_FilePtr = fopen(argv[1], "r");
    }
    And call with:
    Code:
    int main(int argc, char *argv[])
    {
      FILE *fptr=NULL;
      OpenF(&fptr, argv);
      /* blah blah blah */
      fclose(fptr);
      return 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    the FILE structure wasn't intended to be used directly - you should treat it as an opaque handle to an open file.

    Code:
    FILE * fp = fopen(fname, "rb");
    if(fp) {
      /* do some work */
     fclose(fp);
     fp = NULL; /* a good idea if you're reusing the pointer */
     }
    if you want to change the value of the file pointer from within a function, you'll need to pass a pointer to it:

    Code:
    void foo(FILE ** fp, char * fname) {
     if(*fp) {
      fclose(*fp);
      }
     *fp = fopen(fname, "rb");
     / * etc */
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    the FILE structure wasn't intended to be used directly
    I'm sorry but I don't see where this is being done.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I'm sorry but I don't see where this is being done.

    me neither - nevermind.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok just wanted to make sure I wasn't going more insane

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    Okay, thanks alot.

    I know this sound's very stupid, but I'm not quite aware what two stars mean. Is that a pointer to a pointer? or what?

    Thanks.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Is that a pointer to a pointer?
    sure is.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    So, in theory it's used as:

    int Variable;
    int *Pntr;
    int **Pntr2Pntr;

    Pntr = &Variable;
    Pntr2Pnr = &Pntr;

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    Another one

    Thanks for the help. The functions for opening my files now run as sweet as a nut!

    I've encountered a similar problem though. When I try to use a double pointer for a function that reads the file, I get an incompatable pointer error.

    ReadGroup(&InfilePtr);


    ...


    void ReadGroup(FILE **PtrfilePtr)
    {
    char toWrite;

    while (toWrite != EOF)
    {
    toWrite = getc(PtrfilePtr);
    printf("%c", toWrite);
    }
    }


    This is pretty much exactly what I'm doing. Can anyone shed some light?

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Here try reading this post. It might answer your question. BTW, ignore my comment, I was being stupid that day but Prelude kindly pointed that out for me
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  12. #12
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Metalix
    So, in theory it's used as:

    int Variable;
    int *Pntr;
    int **Pntr2Pntr;

    Pntr = &Variable;
    Pntr2Pnr = &Pntr;
    MISSPELLING ALERT!!! ERROR CODE 79287349816716874
    AT LINE 6!!!
    Code:
    Pntr2Pnr = &Pntr; <------ AAHHH!!!
    SHOULD BE
    Code:
    Pntr2Pntr = &Pntr;
    Phew! *sips some coffee*

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    It doesn't seem to answer my questions. See, I can't see what I'm doing wrong. I passed the parameter as a pointer to a pointer, I gave the function the address, why would it be giving me an incompatabilty error. The code worked in the main function.

  14. #14
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    alright, just a simple misunderstanding. Hopefully I will be able to clear it up:

    Code:
    void ReadGroup(FILE **PtrfilePtr)
    You are passing ReadGroup a pointer to a pointer to a file. Thus the pointer to a file is obtained by dereferencing it once, aka *PtrfilePtr. So the rest of your code should look like:

    Code:
    {
            char toWrite;
    
            while (toWrite != EOF)
            {
                    toWrite = getc(*PtrfilePtr);
                    printf("%c", toWrite);
            }
    }
    Let me know if this helps.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    I haven't tried it yet (on another machine), but I think you've hit it spot on.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM