Thread: why is FILE ** needed to open files in separate function?

  1. #1
    olive9
    Guest

    why is FILE ** needed to open files in separate function?

    if you send a file pointer to a function to be opened, you need to use FILE ** in the function parameters and reference the pointer as myfile*. why the double pointer?

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    25
    if you do not pass the pointer by address, you are passing it by value, which means a copy of the pointer is placed on the stack. If you try to allocate memory or open files from within the function scope, the pointer(copy) is lost when the function goes out of scope, and you have no way of finding the file or memory...

    You don't have to pass the pointer in by address, but you will have to return a copy of the FILE* from the function.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Originally posted by [EMOBA]
    You don't have to pass the pointer in by address, but you will have to return a copy of the FILE* from the function.
    It all depends on how the function is coded :
    Code:
    void OpenStream( FILE** fp, char* mode, char* fileName )
    {
    ..
    ..
    }
    
    //Or the second way
    
    FILE* OpenStream( char* mode, char* fileName )
    {
    ..
    ..
    }
    Here's how both would be used :
    Code:
    //For the first method
    FILE *fp1;
    OpenStream(&fp1,"r","blah");
    
    //For the second method
    FILE *fp1;
    fp1 = OpenStream("r","blah");

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    when i'm passing the file pointer to the function that will open the file, i do the following:

    Code:
    long openTransactionFile( FILE **pfFile )
    {
    	long lRetCode = SUCCESS;		
    
    	/* open the file for reading */
    	*pfFile = fopen( TRANSACTION_FILE_NAME, "r" );
    
    	if ( *pfFile == NULL )
    	{
    	   printf( "openTransactionFile: %s file does not exist \n");
    	   lRetCode = FAILURE;
    	}
    
    	/* return status	*/
    	return(lRetCode);
    }
    i'm not returning the file to the calling function, just the return status. i haven't gotten into memory allocation with regards to opening files, so it appears that i need to do some research in that area. thanks for the replies.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your code looks fine. I just test it and was right. Could you further explain what is confusing you?

  6. #6
    snut
    Guest
    she/he was wondering why it was necessary to pass a pointer to a pointer into the function, instead of just passing the pointer...

  7. #7
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Don't use double indirection here. Not necessary. Not correct. As you've shown, it confuses you and that's because it's not warranted in this instance.

    Reprototype with just FILE* -- the address of your file pointer.
    It is not the spoon that bends, it is you who bends around the spoon.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually for what you are doing you do need a pointer to a FILE pointer. Here is why:

    FILE *file: is a 32-bit number that is the address of a file.

    &file: would mean the address of that pointer.

    In order for a function to set the actual value of a pointer (not the data it is pointed to) you need to have a pointer to it. So once again, your function is 100% correct. Keep up the good work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function to open file?
    By Abda92 in forum C Programming
    Replies: 1
    Last Post: 10-27-2007, 07:50 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM