losing my file pointer

W

W. Van Hooste

Hello,

I'm writing a program to display somme values as nice X graphics for a
special purpose. Under here is part of my main code and part of the
code to gather the sample data. the sample data can be gathered in
many ways (mostly API's) but i'm experimenting with a datafile right
now. The data file looks like this :
8<-----------
Unclassified 0
Class_3a 22
System 6
-
Unclassified 2
Class_3a 21
System 7
-
----------->8
In the module i open the file, read formated data, return it, and draw
in my main program. This works nice for the first set (3+1 lines), but
if i come back to the while loop in my main program and want to gather
the second set of data (startig with "Unclassified 2") my file pointer
(fpt) in module1 is lost, it is sometimes changed to another value or
changed to 0x0!


What am i missing here??


main program:

183 void timerCB(XtPointer client_data,XtIntervalId* id)
184 {
185 MyData data;
186 int sampledata[MAX_CLASSES];
187 int class, count;
188
189 data = (MyData) client_data;
190 /* printf("Timer CB\n"); */
191 printf("------\n");
192
193 count=GetSampleCount();
194 class=count;
195 while (--class>=0) {
196 sampledata[class]=GetSampleData(class);
197 printf("%d \n",sampledata[class]); /* debug */
198 };
199 AddSample(data, count, sampledata);
200
201 /* PrintSampleStruct(data); */
202
203 XClearArea(XtDisplay(data->drawa),XtWindow(data->drawa),1,1,data->width,data->height,TRUE);
204 XtAppAddTimeOut(data->app_context,REFRESH,timerCB,data);
205 }

module1:

32 int
33 GetSampleData(int sample)
34 {
35 FILE *fpt;
36 char str[80];
37 int sval;
38
39 /*
40 * return (int)(random()%20);
41 */
42
43 if (filestat == 0) {
44 fpt = fopen("wlmdata.out", "r");
45 filestat = 1;
46 };
47
48
49 if (fscanf(fpt, "%s %u", str, &sval) != -1) {
50 if (str[0] != '-') {
51 printf("%s = %u \n", str, sval); /* debug */
52 return (int) (sval);
53 };
54 return (int) (0);
55 } else {
56 fclose(fpt);
57 };
58
59 }

Thx,
W. Van Hooste.
 
J

Jens.Toerring

W. Van Hooste said:
In the module i open the file, read formated data, return it, and draw
in my main program. This works nice for the first set (3+1 lines), but
if i come back to the while loop in my main program and want to gather
the second set of data (startig with "Unclassified 2") my file pointer
(fpt) in module1 is lost, it is sometimes changed to another value or
changed to 0x0!
32 int
33 GetSampleData(int sample)
34 {
35 FILE *fpt;

Since fpt isn't declared as static its content will get lost the
moment you leave the function. The next time you come along fpt
will have a (more or less) random value. That happens with all
automatic variables of local scope.

Use instead

static FILE *fpt = NULL;
36 char str[80];
37 int sval;
38
39 /*
40 * return (int)(random()%20);
41 */
42
43 if (filestat == 0) {

Where is filestat defined?

You better do here

if ( fpt == NULL )
fpt = fopen("wlmdata.out", "r");

and reset fpt to NULL after you closed the file. In this case you don't
need the additional filestat variable. BTW, you should check if fopen()
didn't return NULL, which would happen if the file can't be opened for
some reason.
44 fpt = fopen("wlmdata.out", "r");
45 filestat = 1;
46 };
47
48
49 if (fscanf(fpt, "%s %u", str, &sval) != -1) {

You may better check here if fscanf() returned 2, because that's the
number of items you seem to expect. An "%u" is for unsigned ints,
but sval is a normal int...
50 if (str[0] != '-') {
51 printf("%s = %u \n", str, sval); /* debug */
52 return (int) (sval);

There's no need for the cast and for the parentheses around the 0,

return sval;

will do (and won't make other people wonder WTF you're doing here).
53 };
54 return (int) (0);

return 0;

is just fine...
55 } else {
56 fclose(fpt);

Set fpt to NULL here to indicate that the file has been closed.
57 };
58
59 }

You forgot to return a value here.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
P

Paul D. Boyle

: if i come back to the while loop in my main program and want to gather
: the second set of data (startig with "Unclassified 2") my file pointer
: (fpt) in module1 is lost, it is sometimes changed to another value or
: changed to 0x0!


: What am i missing here??

As Jens pointed out in a previous post, your FILE * variable is
getting lost when you return from the function which calls fopen().
Jens suggested using a 'static' declaration for your FILE * pointer.
This has some disadvantages, and I'll suggest another way.


: module1:

: 32 int
: 33 GetSampleData(int sample)
: 34 {

If you can change the interface of this function, you make it like this:

FILE *GetSampleData( int sample )

With this function declaration the calling function needs to keep track
of the FILE pointer. You can stick a FILE * variable in your structure
'MyData' to logically associate the FILE pointer with the task at hand.


: 35 FILE *fpt;
: 36 char str[80];
: 37 int sval;
: 38
: 39 /*
: 40 * return (int)(random()%20);
: 41 */

The commented out code is puzzling.

: 42
: 43 if (filestat == 0) {
: 44 fpt = fopen("wlmdata.out", "r");
: 45 filestat = 1;
: 46 };
: 47
: 48
: 49 if (fscanf(fpt, "%s %u", str, &sval) != -1) {

Check the return value for fscanf(), as Jens mentioned.

: 50 if (str[0] != '-') {
: 51 printf("%s = %u \n", str, sval); /* debug */
: 52 return (int) (sval);
: 53 };
: 54 return (int) (0);
: 55 } else {
: 56 fclose(fpt);
: 57 };

Before the function returns, use a statement like this:

return fpt;

: 58
: 59 }

: Thx,
: W. Van Hooste.
 
J

Jens.Toerring

Paul D. Boyle said:
: if i come back to the while loop in my main program and want to gather
: the second set of data (startig with "Unclassified 2") my file pointer
: (fpt) in module1 is lost, it is sometimes changed to another value or
: changed to 0x0!
: What am i missing here??
As Jens pointed out in a previous post, your FILE * variable is
getting lost when you return from the function which calls fopen().
Jens suggested using a 'static' declaration for your FILE * pointer.
This has some disadvantages, and I'll suggest another way.
: module1:
: 32 int
: 33 GetSampleData(int sample)
: 34 {
If you can change the interface of this function, you make it like this:
FILE *GetSampleData( int sample )
With this function declaration the calling function needs to keep track
of the FILE pointer. You can stick a FILE * variable in your structure
'MyData' to logically associate the FILE pointer with the task at hand.

Sorry for the nitpick, but then the OP will also need a way to pass
the FILE pointer back into the function the second time it is called
and he also must think up a different way to return data he just read
from the file (which was the original return value). Of course, that's
all not too complicated to implement but the OP should be aware of
these additional requirements when he follows your suggestion, other-
wise he might end up with the false impression that just by returning
the FILE pointer the local 'fpt' variable would somehow would remain
valid even though he leaves the function.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
W

W. Van Hooste

(e-mail address removed) (W. Van Hooste) wrote in message
*snip*

Thanks for the clear and enlightening reply's.
The module code was [of course] a draft and checks needed to be done.
But anyway, i specialy liked the hints and nitpicking on these ;-)

Sometimes a self-sdudy person needs some personal guiding. Two way
communication is nessesary for educational purposes and you guys do
that verry well!
 
P

Paul D. Boyle

(e-mail address removed)-berlin.de wrote:
:> : if i come back to the while loop in my main program and want to gather
:> : the second set of data (startig with "Unclassified 2") my file pointer
:> : (fpt) in module1 is lost, it is sometimes changed to another value or
:> : changed to 0x0!

:> : What am i missing here??

:> As Jens pointed out in a previous post, your FILE * variable is
:> getting lost when you return from the function which calls fopen().

[snip]

: Sorry for the nitpick, but then the OP will also need a way to pass
: the FILE pointer back into the function the second time it is called
: and he also must think up a different way to return data he just read
: from the file (which was the original return value).

I appreciate the nitpick; it make me more conscientious. Maybe his
function could take his 'Mydata' type as an argument.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top