fgetpos not returning right value

V

Vijay

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
 
B

Ben Bacarisse

Vijay said:
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?
 
S

Seebs

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
 
S

santosh

Vijay said:
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.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top