losing my file pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • W. Van Hooste

    losing my file pointer

    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 "Unclassifi ed 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(XtPoint er client_data,XtI ntervalId* 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=GetSample Count();
    194 class=count;
    195 while (--class>=0) {
    196 sampledata[class]=GetSampleData( class);
    197 printf("%d \n",sampledat a[class]); /* debug */
    198 };
    199 AddSample(data, count, sampledata);
    200
    201 /* PrintSampleStru ct(data); */
    202
    203 XClearArea(XtDi splay(data->drawa),XtWindo w(data->drawa),1,1,dat a->width,data->height,TRUE) ;
    204 XtAppAddTimeOut (data->app_context,RE FRESH,timerCB,d ata);
    205 }

    module1:

    32 int
    33 GetSampleData(i nt 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.
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: losing my file pointer

    W. Van Hooste <wvanhooste@yah oo.com> wrote:[color=blue]
    > 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 "Unclassifi ed 2") my file pointer
    > (fpt) in module1 is lost, it is sometimes changed to another value or
    > changed to 0x0![/color]
    [color=blue]
    > 32 int
    > 33 GetSampleData(i nt sample)
    > 34 {
    > 35 FILE *fpt;[/color]

    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;
    [color=blue]
    > 36 char str[80];
    > 37 int sval;
    > 38
    > 39 /*
    > 40 * return (int)(random()% 20);
    > 41 */
    > 42
    > 43 if (filestat == 0) {[/color]

    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.
    [color=blue]
    > 44 fpt = fopen("wlmdata. out", "r");
    > 45 filestat = 1;
    > 46 };
    > 47
    > 48
    > 49 if (fscanf(fpt, "%s %u", str, &sval) != -1) {[/color]

    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...
    [color=blue]
    > 50 if (str[0] != '-') {
    > 51 printf("%s = %u \n", str, sval); /* debug */
    > 52 return (int) (sval);[/color]

    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).
    [color=blue]
    > 53 };
    > 54 return (int) (0);[/color]

    return 0;

    is just fine...
    [color=blue]
    > 55 } else {
    > 56 fclose(fpt);[/color]

    Set fpt to NULL here to indicate that the file has been closed.
    [color=blue]
    > 57 };
    > 58
    > 59 }[/color]

    You forgot to return a value here.
    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oe rring

    Comment

    • Paul D. Boyle

      #3
      Re: losing my file pointer

      W. Van Hooste <wvanhooste@yah oo.com> wrote:
      : if i come back to the while loop in my main program and want to gather
      : the second set of data (startig with "Unclassifi ed 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(i nt 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.

      --
      Paul D. Boyle
      boyle@laue.chem .ncsu.edu
      North Carolina State University

      Comment

      • clord@clc.net

        #4
        Re: losing my file pointer

        On 18 Sep 2003, W. Van Hooste wrote:

        Your missing the point(er).

        Hahahhahahah ....

        [color=blue]
        >
        >
        > 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 "Unclassifi ed 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(XtPoint er client_data,XtI ntervalId* 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=GetSample Count();
        > 194 class=count;
        > 195 while (--class>=0) {
        > 196 sampledata[class]=GetSampleData( class);
        > 197 printf("%d \n",sampledat a[class]); /* debug */
        > 198 };
        > 199 AddSample(data, count, sampledata);
        > 200
        > 201 /* PrintSampleStru ct(data); */
        > 202
        > 203 XClearArea(XtDi splay(data->drawa),XtWindo w(data->drawa),1,1,dat a->width,data->height,TRUE) ;
        > 204 XtAppAddTimeOut (data->app_context,RE FRESH,timerCB,d ata);
        > 205 }
        >
        > module1:
        >
        > 32 int
        > 33 GetSampleData(i nt 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.
        >[/color]

        Comment

        • Jens.Toerring@physik.fu-berlin.de

          #5
          Re: losing my file pointer

          Paul D. Boyle <boyle@laue.che m.ncsu.edu> wrote:[color=blue]
          > W. Van Hooste <wvanhooste@yah oo.com> wrote:
          > : if i come back to the while loop in my main program and want to gather
          > : the second set of data (startig with "Unclassifi ed 2") my file pointer
          > : (fpt) in module1 is lost, it is sometimes changed to another value or
          > : changed to 0x0![/color]
          [color=blue]
          > : What am i missing here??[/color]
          [color=blue]
          > 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.[/color]
          [color=blue]
          > : module1:[/color]
          [color=blue]
          > : 32 int
          > : 33 GetSampleData(i nt sample)
          > : 34 {[/color]
          [color=blue]
          > If you can change the interface of this function, you make it like this:[/color]
          [color=blue]
          > FILE *GetSampleData( int sample )[/color]
          [color=blue]
          > 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.[/color]

          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
          --
          _ _____ _____
          | ||_ _||_ _| Jens.Toerring@p hysik.fu-berlin.de
          _ | | | | | |
          | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
          \___/ens|_|homs|_|oe rring

          Comment

          • Jack Klein

            #6
            Re: losing my file pointer

            On Thu, 18 Sep 2003 21:07:10 GMT, "clord@clc. net"
            <clord@thelamp. bc.hsia.telus.n et> wrote in comp.lang.c:
            [color=blue]
            > On 18 Sep 2003, W. Van Hooste wrote:
            >
            > Your missing the point(er).
            >
            > Hahahhahahah ....
            >
            >[color=green]
            > >
            > >
            > > Hello,
            > >
            > > I'm writing a program to display somme values as nice X graphics for a[/color][/color]

            [snip]

            Well, well, well. A top-posting troll.

            *plonk*

            --
            Jack Klein
            Home: http://JK-Technology.Com
            FAQs for
            comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
            comp.lang.c++ http://www.parashift.com/c++-faq-lite/
            alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

            Comment

            • W. Van Hooste

              #7
              Re: losing my file pointer

              wvanhooste@yaho o.com (W. Van Hooste) wrote in message news:<495823bb. 0309180316.1282 71af@posting.go ogle.com>...

              *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!

              Comment

              • Paul D. Boyle

                #8
                Re: losing my file pointer

                Jens.Toerring@p hysik.fu-berlin.de wrote:
                : Paul D. Boyle <boyle@laue.che m.ncsu.edu> wrote:
                :> W. Van Hooste <wvanhooste@yah oo.com> wrote:
                :> : if i come back to the while loop in my main program and want to gather
                :> : the second set of data (startig with "Unclassifi ed 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

                --
                Paul D. Boyle
                boyle@laue.chem .ncsu.edu
                North Carolina State University

                Comment

                Working...