By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

File pointer problem

P: n/a
Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
{
/* print addr of file (2)*/
file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/
}

void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(fileptr);
/* print addr of fileptr again (4) */
}

(1) Fileptr address becomes XXX
(2) File addr becomes XXX
(3) File addr becomes YYY
(4) File addr becomes XXX

The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?

Regards,
Nergal

Mar 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11**********************@l75g2000hse.googlegroups .com>,
nergal <ne****@nergal.sewrote:
>Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
{
/* print addr of file (2)*/
file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/
}

void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(fileptr);
/* print addr of fileptr again (4) */
}

(1) Fileptr address becomes XXX
(2) File addr becomes XXX
(3) File addr becomes YYY
(4) File addr becomes XXX

The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?
Because C doesn't work that way.

Mar 15 '07 #2

P: n/a
nergal wrote:
Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
[...]
This is Question 4.8 in the comp.lang.c Frequently Asked
Questions (FAQ) list, <http://c-faq.com/>.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 15 '07 #3

P: n/a
nergal wrote:
Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
{
/* print addr of file (2)*/
file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/
}

void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(fileptr);
/* print addr of fileptr again (4) */
}

(1) Fileptr address becomes XXX
(2) File addr becomes XXX
(3) File addr becomes YYY
(4) File addr becomes XXX

The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?
Why should it? Pointers in C are just values.

If you want to /change/ a FILE* variable, you'll have to
pass it's /address/, which is of type FILE**.

--
Chris "electric hedgehog" Dollin
A rock is not a fact. A rock is a rock.

Mar 15 '07 #4

P: n/a
nergal wrote:
I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
{
/* print addr of file (2)*/
file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/
}

void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(fileptr);
/* print addr of fileptr again (4) */
}

(1) Fileptr address becomes XXX
(2) File addr becomes XXX
(3) File addr becomes YYY
(4) File addr becomes XXX

The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?
void func(FILE **file)
{
/* print addr of file (2)*/
*file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/
}
void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(&fileptr);
/* print addr of fileptr again (4) */
}
--
Nick Keighley

Mar 15 '07 #5

P: n/a
On Mar 15, 7:34 am, "nergal" <ner...@nergal.sewrote:
Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
{
/* print addr of file (2)*/
file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/

}

void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(fileptr);
/* print addr of fileptr again (4) */

}

(1) Fileptr address becomes XXX
(2) File addr becomes XXX
(3) File addr becomes YYY
(4) File addr becomes XXX

The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?
Because you are trying to change the value of filePtr, which is a
FILE* object, not a FILE object. Therefore, you need to pass a
pointer to filePtr, or a FILE** object:

void func(FILE **theFile)
{
*theFile = fopen("theFile", "wb");
}

void callingFunc(void)
{
FILE *filePtr;

func(&filePtr);
...
}

Mar 15 '07 #6

P: n/a
nergal wrote:
Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
{
/* print addr of file (2)*/
file = fopen("thefile", "wb"); /* open as binary */
/* print addr of file (3)*/
}

void callingFunc(void)
{
FILE* fileptr;

/* print addr of fileptr (1)*/
func(fileptr);
/* print addr of fileptr again (4) */
}

#include <stdio.h>
#define FNAME "thefile"
void openfile(FILE ** file)
{
printf("%s.%d: *file = %p\n", __func__, __LINE__, (void *) *file);
*file = fopen(FNAME, "wb"); /* open as binary */
printf("%s.%d: *file = %p\n", __func__, __LINE__, (void *) *file);
}

void closeandremove(FILE ** file)
{
printf("%s.%d: *file = %p\n", __func__, __LINE__, (void *) *file);
printf("fclose returned %d\n", fclose(*file));
printf("remove returned %d\n", remove(FNAME));
printf("%s.%d: *file = %p\n", __func__, __LINE__, (void *) *file);
*file = 0;
printf("%s.%d: *file = %p\n", __func__, __LINE__, (void *) *file);
}

int main(void)
{
FILE *fileptr = 0;
printf("[output]\n");
printf("%s.%d: fileptr = %p\n", __func__, __LINE__,
(void *) fileptr);
openfile(&fileptr);
printf("%s.%d: fileptr = %p\n", __func__, __LINE__,
(void *) fileptr);
if (fileptr) {
closeandremove(&fileptr);
printf("%s.%d: fileptr = %p\n", __func__, __LINE__,
(void *) fileptr);
}
else
printf("openfile() failed\n");
return 0;
}

[output]
main.26: fileptr = 0
openfile.7: *file = 0
openfile.9: *file = 20d98
main.29: fileptr = 20d98
closeandremove.14: *file = 20d98
fclose returned 0
remove returned 0
closeandremove.17: *file = 20d98
closeandremove.19: *file = 0
main.33: fileptr = 0
>
(1) Fileptr address becomes XXX
(2) File addr becomes XXX
(3) File addr becomes YYY
(4) File addr becomes XXX

The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?
Because you don't understand how arguments are passed to functions.
This is covered not only in the FAQ, but in any elementary C textbook.
Mar 15 '07 #7

P: n/a
Chris Dollin wrote:
>
nergal wrote:
Hi,

I've been staring myself blind on the same little code now and I cant
see the problem. The filepointer seems to not change when I open it in
a function.
I've written like this (a bit pseudo here):

void func(FILE* file)
[...]
void callingFunc(void)
{
FILE* fileptr;
[...]
func(fileptr);
[...]
}
[...]
The correct address of the file pointer must be YYY since its in that
function I open it. But why doesn't it change in the calling function
since I send in a pointer?

Why should it? Pointers in C are just values.

If you want to /change/ a FILE* variable, you'll have to
pass it's /address/, which is of type FILE**.
Or, rather than passing the address of the FILE* to func(), change
func() to return the FILE*.

FILE *func(void)
{
FILE *file;
...
file = fopen("thefile","wb");
...
return file;
}

void callingFunc(void)
{
FILE *fileptr;
...
fileptr = func();
...
}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Mar 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.