![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 3
| 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 | |
| | #2 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #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;
}
|
| smitchell is offline | |
| | #4 |
| and the Hat of Guessing 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |