Thread: Problems passing a file pointer to functions

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    3

    Question Problems passing a file pointer to functions

    Hi,

    I'm having problems passing a file pointer through nested functions.

    In the code below, main calls func1 which calls func1a which calls openFile. With some modification to the code (to flush and close the file) openFile can write to the file it has opened.

    If func1a is called with the file pointer initialised to NULL, the file pointer is not null before returning from the function. func1a can't write to the file, presumably because the pointer is pointing at something other than the file, but calling fprintf with the file pointer doesn't generate a segmentation fault.

    Testing the file pointer in func1 returns NULL. Obviously because of this attempting to write to the file later in the program causes a segmentation fault.

    I know the problem has to do with the way I'm passing the file pointer and/or the struct which contains it between functions. I can be fairly certain of this because originally, openFile took a file pointer as a parameter rather than returning it, and testing the file pointer in func1a returned NULL.

    I've read several forum posts and tutorials that say that, because a file pointer is already a pointer, it should be passed as-is rather than passing a pointer to it. This doesn't seem to work for me, and I've spent the last few weeks messing around with pointers and pointers to pointers, still with the same results.

    When compiling the code using lcc under Windows XP, I get the warning
    Warning d:\testpointers.c: 39 Missing prototype for 'openFile'
    which refers to the line myFile = openFile();

    Thanks in advance for any help you can give me.

    Code:
    #include <stdio.h>
    
    typedef struct
    {
    	FILE *myFile;
    } myStruct;
    
    FILE* openFile()
    {
    	FILE *file = fopen("D:\\test1.txt","w");
    	if(file == NULL)
    		printf("openFile: file is null\n");
    	return file;
    }
    
    int func1a(FILE *myFile)
    {
    	myFile = openFile();
    	if(myFile == NULL)
    		printf("func1a: file is null\n");
    	return 0;
    }
    
    int func1(myStruct *a)
    {
    	int status = func1a(a->myFile);
    	if(a->myFile == NULL)
    		printf("func1: file is null\n");
    	return 0;
    }
    
    int main()
    {
    	myStruct a;
    	a.myFile = NULL;
    	int status = 0;
    	status = func1(&a);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    To modify a pointer, you need a pointer to a pointer.

    One of your functions needs to receive a FILE** parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    Thanks for the quick response. After following your advice, I've modified the code to look like this:

    Code:
    int openFile(FILE ***file)
    {
    	**file = fopen("D:\\test1.txt","w");
    	if(**file == NULL)
    		printf("openFile: file is null\n");
    	return 0;
    }
    
    int func1a(FILE **myFile)
    {
    	openFile(&myFile);
    	if(*myFile == NULL)
    		printf("func1a: file is null\n");
    	return 0;
    }
    
    int func1(myStruct *a)
    {
    	int status = func1a(&a->myFile);
    	if(&a->myFile == NULL)
    		printf("func1: file is null\n");
    	return 0;
    }
    It works fine, but is having a pointer to a pointer to a file pointer in openFile the best way to achieve what I want?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know why you went from 1 to 3, and skipped 2:
    Code:
    int openFile(FILE **file)
    {
    	*file = fopen("D:\\test1.txt","w");
    	if(*file == NULL)
    		printf("openFile: file is null\n");
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    3
    No, neither do I . I think it was because, when I tried two originally, another part of the program stopped working and I put two and two together and came up with 6. Two works fine now though. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing FIle pointer as argt
    By ganesh bala in forum C Programming
    Replies: 2
    Last Post: 04-08-2009, 02:56 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. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM