C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-28-2008, 06:55 AM   #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);
}
smitchell is offline   Reply With Quote
Old 09-28-2008, 08:01 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
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.

Salem is offline   Reply With Quote
Old 09-30-2008, 02:05 PM   #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?
smitchell is offline   Reply With Quote
Old 09-30-2008, 02:08 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
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;
}
tabstop is offline   Reply With Quote
Old 09-30-2008, 02:29 PM   #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.
smitchell is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing FIle pointer as argt ganesh bala C Programming 2 04-08-2009 02:56 AM
Problems with file pointer in functions willie C Programming 6 11-23-2008 01:54 PM
Can we have vector of vector? ketu1 C++ Programming 24 01-03-2008 05:02 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Possible circular definition with singleton objects techrolla C++ Programming 3 12-26-2004 10:46 AM


All times are GMT -6. The time now is 11:31 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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