direct access with fpos_t doesn´t work

Discussion in 'C Programming' started by C. Sengstock, Oct 8, 2003.

  1. C. Sengstock

    C. Sengstock Guest

    Hi,
    i want to save the keywords of an ini file in a struct, together with a
    fpos_t. I think i´m right with the concept, but the access through fsetpos()
    doesn´t work. The position is always wrong (except, in this example, the
    first line).

    My struct looks like this:
    ***
    typedef struct {
    char pname[32];
    fpos_t pos;
    } iniParam;

    This routine reads the data and put it to "iniParam param[100]":
    ***
    while (!feof (pFile)) {
    fgetpos(pFile, &pos);
    c=fgetc(pFile);
    ungetc(c, pFile);
    if(c == '#')
    fgets (linebuf, 255, pFile);
    else {
    param.pos = pos;
    fgets (linebuf, 255, pFile);
    sscanf(linebuf, "%s", param.pname);
    i++;
    }
    }

    This routine reads the keywords and do a corresponding fscanf:
    ***
    for(i=0; i<n; i++) {
    if(strcmp(param.pname, "keyword1") == 0) {
    pos = param.pos;
    fsetpos(pFile, &pos);
    fscanf(pFile, "%*s %d", &param1);
    }
    if(strcmp(param.pname, "keyword2") == 0) {
    ...

    Don´t know why the access through a fpos_t position doesn´t work.
    Thanks for any help, Chris
     
    C. Sengstock, Oct 8, 2003
    #1
    1. Advertising

  2. C. Sengstock

    Nick Austin Guest

    On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <>
    wrote:

    >Hi,
    >i want to save the keywords of an ini file in a struct, together with a
    >fpos_t. I think i´m right with the concept, but the access through fsetpos()
    >doesn´t work. The position is always wrong (except, in this example, the
    >first line).


    It works for me with just two problems. The reading loop doesn't
    detect end of file correctly as explained in the faq:

    http://www.eskimo.com/~scs/C-faq/q12.2.html

    Also blank lines and junk lines in the ini file result in invalid
    entries in param[].

    My advice is:
    1. Check the return code of each library functions you call.
    2. Post a complete but minimalist program, including the ini
    file contents that demonstrate the problem.

    >My struct looks like this:
    >***
    >typedef struct {
    > char pname[32];
    > fpos_t pos;
    >} iniParam;
    >
    >This routine reads the data and put it to "iniParam param[100]":
    >***
    >while (!feof (pFile)) {
    > fgetpos(pFile, &pos);
    > c=fgetc(pFile);
    > ungetc(c, pFile);
    > if(c == '#')
    > fgets (linebuf, 255, pFile);
    > else {
    > param.pos = pos;
    > fgets (linebuf, 255, pFile);
    > sscanf(linebuf, "%s", param.pname);
    > i++;
    > }
    >}
    >
    >This routine reads the keywords and do a corresponding fscanf:
    >***
    >for(i=0; i<n; i++) {
    > if(strcmp(param.pname, "keyword1") == 0) {
    > pos = param.pos;
    > fsetpos(pFile, &pos);
    > fscanf(pFile, "%*s %d", &param1);
    > }
    > if(strcmp(param.pname, "keyword2") == 0) {
    > ...
    >
    >Don´t know why the access through a fpos_t position doesn´t work.
    >Thanks for any help, Chris


    Nick.
     
    Nick Austin, Oct 8, 2003
    #2
    1. Advertising

  3. C. Sengstock

    hongky Guest

    about
    http://www.eskimo.com/~scs/C-faq/q12.2.html
    u can
    while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))
    {

    }

    "Nick Austin" <> wrote in message
    news:...
    > On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <>
    > wrote:
    >
    > >Hi,
    > >i want to save the keywords of an ini file in a struct, together with a
    > >fpos_t. I think i´m right with the concept, but the access through

    fsetpos()
    > >doesn´t work. The position is always wrong (except, in this example, the
    > >first line).

    >
    > It works for me with just two problems. The reading loop doesn't
    > detect end of file correctly as explained in the faq:
    >
    > http://www.eskimo.com/~scs/C-faq/q12.2.html
    >
    > Also blank lines and junk lines in the ini file result in invalid
    > entries in param[].
    >
    > My advice is:
    > 1. Check the return code of each library functions you call.
    > 2. Post a complete but minimalist program, including the ini
    > file contents that demonstrate the problem.
    >
    > >My struct looks like this:
    > >***
    > >typedef struct {
    > > char pname[32];
    > > fpos_t pos;
    > >} iniParam;
    > >
    > >This routine reads the data and put it to "iniParam param[100]":
    > >***
    > >while (!feof (pFile)) {
    > > fgetpos(pFile, &pos);
    > > c=fgetc(pFile);
    > > ungetc(c, pFile);
    > > if(c == '#')
    > > fgets (linebuf, 255, pFile);
    > > else {
    > > param.pos = pos;
    > > fgets (linebuf, 255, pFile);
    > > sscanf(linebuf, "%s", param.pname);
    > > i++;
    > > }
    > >}
    > >
    > >This routine reads the keywords and do a corresponding fscanf:
    > >***
    > >for(i=0; i<n; i++) {
    > > if(strcmp(param.pname, "keyword1") == 0) {
    > > pos = param.pos;
    > > fsetpos(pFile, &pos);
    > > fscanf(pFile, "%*s %d", &param1);
    > > }
    > > if(strcmp(param.pname, "keyword2") == 0) {
    > > ...
    > >
    > >Don´t know why the access through a fpos_t position doesn´t work.
    > >Thanks for any help, Chris

    >
    > Nick.
    >
     
    hongky, Oct 8, 2003
    #3
  4. C. Sengstock

    C. Sengstock Guest

    "Nick Austin" wrote:
    > My advice is:
    > 1. Check the return code of each library functions you call.
    > 2. Post a complete but minimalist program, including the ini
    > file contents that demonstrate the problem.


    This is straighter to the point. The code should save the position of a
    line, and do the output through that positions. But the code doesn´t work,
    the output is:
    -----------------------------
    #dontreadthis <<< this is the "myfile.txt" data
    baud = 4
    #nodata

    port = 50
    parity = 6
    <<< till here
    *** output
    #dontreadthis <<<< that´s the generated output through positions
    this
    = 4
    ta
    <<<< that´s the end
    ------------------------------------


    the code looks like this:
    -------------------------------------
    fpos_t position[50];
    FILE* fp;
    char line[200];
    int i, n;

    fp = fopen("myfile.txt", "r");
    if(!fp) perror("error opening file");
    else {
    i=0;
    while(!feof(fp)) {
    if(fgetpos(fp, &position) != 0) {
    printf("error getting position\n");
    exit(1);
    }
    if(fgets(line, 200, fp)==NULL)
    break;
    printf("%s", line);
    i++;
    }
    }
    n=i;
    printf("\n*** output\n");
    for(i=0; i<n; i++) {
    if(fsetpos(fp, &position) != 0) {
    printf("error setting position\n");
    exit(1);
    }
    fgets(line, 200, fp);
    printf("%s", line);
    }
    fclose(fp);
    -----------------------------------------

    Thanks, Chris
     
    C. Sengstock, Oct 8, 2003
    #4
  5. C. Sengstock

    CBFalconer Guest

    Re: direct access with fpos_t =?iso-8859-1?Q?doesn=B4t?= work

    "C. Sengstock" wrote:
    > "Nick Austin" wrote:
    >
    > > My advice is:
    > > 1. Check the return code of each library functions you call.
    > > 2. Post a complete but minimalist program, including the ini
    > > file contents that demonstrate the problem.

    >
    > This is straighter to the point. The code should save the position
    > of a line, and do the output through that positions. But the code
    > doesn´t work, the output is:
    >

    .... snip ...

    All complete C programs include an "int main(...) {" line, and
    code cannot be generated outside of a function, which, in turn,
    usually requires some sort of return statement.

    Try again, with something that can be cut, pasted and compiled
    unchanged. Do include the sample data again - you did that right.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 8, 2003
    #5
  6. C. Sengstock

    C. Sengstock Guest

    "CBFalconer" wrote:
    > All complete C programs include an "int main(...) {" line, and
    > code cannot be generated outside of a function, which, in turn,
    > usually requires some sort of return statement.

    Yep ... copy paste ready cody ...

    #include<stdio.h>
    #include<stdlib.h>
    int main() {
    fpos_t position[50];
    FILE* fp;
    char line[200];
    int i, n;

    fp = fopen("myfile.txt", "r");
    if(!fp) perror("error opening file");
    else {
    i=0;
    while(!feof(fp)) {
    if(fgetpos(fp, &position) != 0) {
    printf("error getting position\n");
    exit(1);
    }
    if(fgets(line, 200, fp)==NULL)
    break;
    printf("%s", line);
    i++;
    }
    }
    n=i;
    printf("\n*** output\n");
    for(i=0; i<n; i++) {
    if(fsetpos(fp, &position) != 0) {
    printf("error setting position\n");
    exit(1);
    }
    fgets(line, 200, fp);
    printf("%s", line);
    }
    fclose(fp);
    return 0;
    }

    Thanks, Chris
     
    C. Sengstock, Oct 8, 2003
    #6
  7. C. Sengstock

    Nick Austin Guest

    On Wed, 8 Oct 2003 14:19:49 +0200, "C. Sengstock" <>
    wrote:

    >This is straighter to the point. The code should save the position of a
    >line, and do the output through that positions. But the code doesn´t work,
    >the output is:
    >-----------------------------
    >#dontreadthis <<< this is the "myfile.txt" data
    >baud = 4
    >#nodata
    >
    >port = 50
    >parity = 6
    > <<< till here
    >*** output
    >#dontreadthis <<<< that´s the generated output through positions
    >this
    >= 4
    >ta
    > <<<< that´s the end
    >------------------------------------


    When I try the output is:

    #dontreadthis
    baud = 4
    #nodata

    port = 50
    parity = 6

    *** output
    #dontreadthis
    baud = 4
    #nodata

    port = 50
    parity = 6

    So the problem isn't in the code snippet you posted. You could
    try asking again in a newsgroup for you compiler.

    >the code looks like this:


    #include <stdio.h>

    int main( void )
    {
    > fpos_t position[50];
    > FILE* fp;
    > char line[200];
    > int i, n;
    >
    > fp = fopen("myfile.txt", "r");
    > if(!fp) perror("error opening file");
    > else {
    > i=0;
    > while(!feof(fp)) {
    > if(fgetpos(fp, &position) != 0) {
    > printf("error getting position\n");
    > exit(1);
    > }
    > if(fgets(line, 200, fp)==NULL)
    > break;
    > printf("%s", line);
    > i++;
    > }
    > }
    > n=i;
    > printf("\n*** output\n");
    > for(i=0; i<n; i++) {
    > if(fsetpos(fp, &position) != 0) {
    > printf("error setting position\n");
    > exit(1);
    > }
    > fgets(line, 200, fp);
    > printf("%s", line);
    > }
    > fclose(fp);

    return 0;
    }

    Nick.
     
    Nick Austin, Oct 8, 2003
    #7
  8. Re: direct access with fpos_t =?iso-8859-1?q?doesn=B4t?= work

    in comp.lang.c i read:

    >while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))


    the feof test is redundant.

    --
    a signature
     
    those who know me have no need of my name, Oct 8, 2003
    #8
  9. C. Sengstock

    nrk Guest

    Re: direct access with fpos_t =?ISO-8859-1?Q?doesn=B4t?= work

    C. Sengstock wrote:

    >
    > "CBFalconer" wrote:
    >> All complete C programs include an "int main(...) {" line, and
    >> code cannot be generated outside of a function, which, in turn,
    >> usually requires some sort of return statement.

    > Yep ... copy paste ready cody ...
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main() {
    > fpos_t position[50];
    > FILE* fp;
    > char line[200];
    > int i, n;
    >
    > fp = fopen("myfile.txt", "r");
    > if(!fp) perror("error opening file");

    You most probably want to exit as well at this point (or atleast avoid
    reading from fp later on down below, by initializing i to 0).

    > else {
    > i=0;
    > while(!feof(fp)) {
    > if(fgetpos(fp, &position) != 0) {
    > printf("error getting position\n");
    > exit(1);

    exit(EXIT_FAILURE);

    > }
    > if(fgets(line, 200, fp)==NULL)

    I would personally prefer to use sizeof(line) instead in this case.

    > break;
    > printf("%s", line);
    > i++;
    > }
    > }
    > n=i;
    > printf("\n*** output\n");
    > for(i=0; i<n; i++) {
    > if(fsetpos(fp, &position) != 0) {
    > printf("error setting position\n");
    > exit(1);

    exit(EXIT_FAILURE);
    > }
    > fgets(line, 200, fp);

    See above.

    > printf("%s", line);
    > }
    > fclose(fp);
    > return 0;
    > }
    >
    > Thanks, Chris


    What exactly is the problem you are encountering with this code? Are you
    100% certain that none of your lines are more than 200 characters in
    length?

    -nrk.
     
    nrk, Oct 8, 2003
    #9
  10. C. Sengstock

    C. Sengstock Guest

    "nrk" wrote:
    > What exactly is the problem you are encountering with this code? Are you
    > 100% certain that none of your lines are more than 200 characters in
    > length?

    Yep, size of line isn´t greater than 200. I really think the problem has to
    do with the compiler (tcc or bcc32, both make this failure!). I tried a lot
    of tests with fgetpos() and fsetpos(), but they all went wrong!

    Chris
     
    C. Sengstock, Oct 8, 2003
    #10
  11. C. Sengstock

    CBFalconer Guest

    Re: direct access with fpos_t =?iso-8859-1?Q?doesn=B4t?= work

    "C. Sengstock" wrote:
    > "CBFalconer" wrote:
    >
    > > All complete C programs include an "int main(...) {" line, and
    > > code cannot be generated outside of a function, which, in turn,
    > > usually requires some sort of return statement.

    >
    > Yep ... copy paste ready cody ...
    >
    > #include<stdio.h>
    > #include<stdlib.h>
    > int main() {


    int main(void) is better, but only a nit.

    > fpos_t position[50];
    > FILE* fp;
    > char line[200];
    > int i, n;
    >
    > fp = fopen("myfile.txt", "r");
    > if(!fp) perror("error opening file");


    here, after detecting the error, you continue with the output.
    This term should contain an exit(EXIT_FAILURE); statement.

    > else {
    > i=0;
    > while(!feof(fp)) {
    > if(fgetpos(fp, &position) != 0) {
    > printf("error getting position\n");
    > exit(1);
    > }
    > if(fgets(line, 200, fp)==NULL)
    > break;
    > printf("%s", line);
    > i++;
    > }
    > }
    > n=i;
    > printf("\n*** output\n");
    > for(i=0; i<n; i++) {
    > if(fsetpos(fp, &position) != 0) {
    > printf("error setting position\n");
    > exit(1);
    > }
    > fgets(line, 200, fp);
    > printf("%s", line);
    > }
    > fclose(fp);
    > return 0;
    > }


    Now I pretty well have to look at it. :) So I took your message,
    snipped off the beginning and end, and:

    [1] c:\c\junk>gcc -o fpos.exe fpos.c

    [1] c:\c\junk>type fpostst.txt
    #dontreadthis
    baud = 4
    #nodata

    port = 50
    parity = 6

    [1] c:\c\junk>fpos
    #dontreadthis
    baud = 4
    #nodata

    port = 50
    parity = 6

    *** output
    #dontreadthis
    baud = 4
    #nodata

    port = 50
    parity = 6

    This is the results here, after changing the input file name. It
    seems to do what you wanted, so you should look to your system.

    Now you see how much easier it is for people to help if you give
    them something to work with. I could criticise the lack of blanks
    around tokens, but I won't :)

    One thought strikes me - does your test file have its last line
    properly terminated with a \n? If not the funny termination
    conditions might be causing the problem.

    Of course the reread tells you nothing, since it is done in
    sequence and thus the fsetpos doesn't have any work to do. Try
    doing the reads in reverse order in the output section, i.e:

    for (i = n-1; i >= 0; i--) {

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 9, 2003
    #11
  12. C. Sengstock

    Chris Torek Guest

    Re: direct access with fpos_t doesn[microsoft-crud]t work

    In article <>
    those who know me have no need of my name <> wrote:
    >in comp.lang.c i read:
    >>while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))

    >the feof test is redundant.


    Actually, it is not redundant: it is wrong. :)

    If fgets() returns NULL, we know that getc(infp) -- or some equivalent
    operation -- returned EOF, but there are *two* reasons that getc()
    might return EOF. One is "end of file", which would also set the
    EOF indicator on the stream, making the feof() call redundant in
    this (usual) case. The other, however, is "error reading stream".
    You might, for instance, have this occur when reading from a bad
    floppy disk. In this case the EOF indicator is *not* set on the
    stream. Instead, the error indicator -- the thing tested by
    ferror(infp) -- is set.

    Thus, when reading from a bad floppy, the loop:

    while (fgets(buf, MAXLINE, infp) != NULL && !feof(infp))

    may run forever, calling fgets(), getting NULL due to ferror(infp),
    and then seeing that feof(infp) is still 0.
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Oct 10, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anan
    Replies:
    8
    Views:
    15,672
    John C. Bollinger
    Dec 8, 2004
  2. MZ
    Replies:
    7
    Views:
    830
    Ed Mullen
    Mar 17, 2008
  3. Tilman
    Replies:
    0
    Views:
    414
    Tilman
    Mar 19, 2008
  4. CJM
    Replies:
    12
    Views:
    352
  5. Bill Cunningham

    fpos_t

    Bill Cunningham, May 1, 2014, in forum: C Programming
    Replies:
    15
    Views:
    135
    James Kuyper
    Jun 3, 2014
Loading...

Share This Page