Passing FIle pointer as argt

This is a discussion on Passing FIle pointer as argt within the C Programming forums, part of the General Programming Boards category; I want to pass input and output file as command line arguments... I want output file pointer have to be ...

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Passing FIle pointer as argt

    I want to pass input and output file as command line arguments...

    I want output file pointer have to be passed as argument to a function..

    How can i do it? Is this Ok??

    But I am getting Error :cannot convert `_iobuf' to `FILE**' for argument `2' to `void encode(char*, FILE**)'

    Code:
    int main(int argc,char *argv[])
    {
        FILE *fp,*fp2;
        char str[80],*str1;
        fp=fopen(argv[1],"r");
        fp2=fopen(argv[2],"w");  // file opened here
    
    ...............
    
     encode(str1,*fp2);
    
    
    
    
    }
    
    
    
             
    void encode(char *str,FILE **fp2)
    { 
        ................
             // Here i want to write it to file
              fprintf(*fp2,"%d",no);
             
    
    }
    Help me how to do please?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The address of operator is "&" not "*".

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But from the looks of it, you shouldn't be using **fp2, and thus not use & either - unless you intend to CHANGE file-pointer in the called function, all you need to pass is the file-pointer itself, as "FILE *fp", and just give it's name as the argument to the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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