Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fgetpos not returning right value

Reply
Thread Tools

fgetpos not returning right value

 
 
Vijay
Guest
Posts: n/a
 
      01-31-2010
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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-31-2010
Vijay <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      01-31-2010
On 2010-01-31, Vijay <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      01-31-2010


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hpricot not returning the right html?? Hannes Rammer Ruby 1 11-03-2008 11:32 AM
Dynamic Menu Items is not right aligned with Right to Left documen =?Utf-8?B?QmlzaG95?= ASP .Net 0 12-28-2006 11:39 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Request.Param not returning a value Nancy. ASP .Net 0 09-30-2004 12:45 PM
EditItem value not returning "" (even when something has been ente =?Utf-8?B?WGF2aWVybw==?= ASP .Net 0 08-02-2004 07:33 PM



Advertisments