fgetpos not returning right value

Discussion in 'C Programming' started by Vijay, Jan 31, 2010.

  1. Vijay

    Vijay Guest

    Hi,

    Can anybody help me in below code? I am not sure how it is happing.
    After fgetc(pB), position_B should be 1 but i am getting 0.
    but I am getting right value for position_A.
    .....
    FILE *pA=NULL, *pB=NULL,
    .....
    fpos_t position_A,position_B;

    while(!feof(pA)&& !feof(pB))
    {
    fgetpos (pA, &position_A); // position_A-> 0
    fgetpos (pB, &position_B); //position_B-> 0
    a=fgetc(pA);
    b=fgetc(pB);
    fgetpos (pA, &position_A); // position_A-> 1
    fgetpos (pB, &position_B); //position_B-> 0
    .....
    I am using Visual Studio 2003 and 2008
    Thanks,
    Vijay
     
    Vijay, Jan 31, 2010
    #1
    1. Advertising

  2. Vijay <> writes:

    > Can anybody help me in below code? I am not sure how it is happing.
    > After fgetc(pB), position_B should be 1 but i am getting 0.
    > but I am getting right value for position_A.


    fpos_t is not always an integer type. Printing its value might be
    confusing.

    > ....
    > FILE *pA=NULL, *pB=NULL,
    > ....
    > fpos_t position_A,position_B;
    >
    > while(!feof(pA)&& !feof(pB))
    > {
    > fgetpos (pA, &position_A); // position_A-> 0
    > fgetpos (pB, &position_B); //position_B-> 0
    > a=fgetc(pA);
    > b=fgetc(pB);
    > fgetpos (pA, &position_A); // position_A-> 1
    > fgetpos (pB, &position_B); //position_B-> 0
    > ....


    There may be some error in what you are doing (other than assuming
    that fpos_t is a number) but there is not enough information here to
    be able to tell. Can you post a minimal compilable program that shows
    the problem?

    --
    Ben.
     
    Ben Bacarisse, Jan 31, 2010
    #2
    1. Advertising

  3. Vijay

    Seebs Guest

    On 2010-01-31, Vijay <> wrote:
    > while(!feof(pA)&& !feof(pB))


    This is wrong. Never do this.

    Test the reads you perform. feof() tells you whether a PREVIOUS read
    ALREADY FAILED. That's not useful to you.

    > fgetpos (pA, &position_A); // position_A-> 0
    > fgetpos (pB, &position_B); //position_B-> 0
    > a=fgetc(pA);
    > b=fgetc(pB);
    > fgetpos (pA, &position_A); // position_A-> 1
    > fgetpos (pB, &position_B); //position_B-> 0


    > I am using Visual Studio 2003 and 2008


    But apparently you never actually opened the files. Or you didn't
    think to offer a complete example. :)

    The obvious thing that occurs to me to ask is whether fgetc()
    worked in both cases. If b were EOF, then this could make sense.

    However, it may also matter how you opened the files.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jan 31, 2010
    #3
  4. Vijay

    santosh Guest

    Vijay wrote:
    > Hi,
    >
    > Can anybody help me in below code? I am not sure how it is happing.
    > After fgetc(pB), position_B should be 1 but i am getting 0.
    > but I am getting right value for position_A.
    > ....
    > FILE *pA=NULL, *pB=NULL,
    > ....
    > fpos_t position_A,position_B;
    >
    > while(!feof(pA)&& !feof(pB))
    > {
    > fgetpos (pA, &position_A); // position_A-> 0
    > fgetpos (pB, &position_B); //position_B-> 0
    > a=fgetc(pA);
    > b=fgetc(pB);
    > fgetpos (pA, &position_A); // position_A-> 1
    > fgetpos (pB, &position_B); //position_B-> 0
    > ....
    > I am using Visual Studio 2003 and 2008


    The first thing you should do is to test the return values of all the
    fgetpos() and fgetc() calls for error. fgetc() returns EOF on end-of-
    file or read error (which you can differentiate by means of an
    immediate call to feof() or ferror()), while fgetpos() returns a non-
    zero value and sets errno to an implementation defined value. The
    point is, you should try to narrow down the point of error by
    elimination. If any of these calls failed, then we can proceed to
    analyse why it did, and for that we'd need the full source for the
    function.

    And don't forget to turn on the compiler switches for ANSI
    conformance, unless your program needs extensions.
     
    santosh, Jan 31, 2010
    #4
    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. Tom
    Replies:
    0
    Views:
    434
  2. =?Utf-8?B?QmlzaG95?=
    Replies:
    0
    Views:
    1,004
    =?Utf-8?B?QmlzaG95?=
    Dec 28, 2006
  3. Bogdan
    Replies:
    1
    Views:
    821
    Bogdan
    Jun 16, 2009
  4. Hannes Rammer

    Hpricot not returning the right html??

    Hannes Rammer, Oct 27, 2008, in forum: Ruby
    Replies:
    1
    Views:
    113
    Hannes Rammer
    Nov 3, 2008
  5. Leon
    Replies:
    3
    Views:
    183
    TaeHo Yoo
    Nov 26, 2004
Loading...

Share This Page