473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

File pointer problem

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
7 5836
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Joe Laughlin | last post by:
I've not used C much before, so I don't know how robust or good this code is. I'd appreciate any feedback or criticisms anyone has! Thanks, Joe #include <stdio.h> #include <string.h>
8
by: Ritesh | last post by:
Hi!!!!!!!!!!! I am a final year engineering student. I am making a final year project on reed-solomon codec in C using VC++ 6.0. The input to the program can be any file that the user wants to...
4
by: Arif | last post by:
My programs searches the header of input barcode in index file. Get the record position next to Barcode header. Then moves the file pointer of products file to reach that record. My products...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
2
revolter00
by: revolter00 | last post by:
hi all, this is a piece of code from the program that i`m currently working on.. . . #define SIZE 10 . . . int opt, NumARR, L, MaxINT, sum=0, input_int;
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
9
by: kumar | last post by:
i am bit confused about this code, why does the below while loop is contineous how can i come out of the file. i am using linux machine char ch;//char varaible FILE *fp; // file pointer...
185
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.