Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > comparison error

Reply
Thread Tools

comparison error

 
 
Bill Cunningham
Guest
Posts: n/a
 
      10-27-2011
I get a comparison error here and I see the problem but don't know how
to fix it. It's been so long. Here's the code.

#include <stdio.h>

int main(int argc, char **argv)
{
if (argc != 2) {
perror("argc error");
return -1;
}
int i, a;
i = a = 0;
FILE *fp;
if ((fp = fopen(argv[1], "r+")) == EOF) {

/* right here above argv[1] is a char ** and the first parameter of fopen is
char *. This is what I believe the problem is. How should I fix this?
*/
perror("fopen error");
return -1;
}
for (; i != EOF; ++i) {
fgetc(fp);
fputc(a, fp);
return 0;
}
fclose(fp);
}


 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      10-28-2011
In article <4ea9f052$0$19267$(E-Mail Removed)>,
Bill Cunningham <(E-Mail Removed)> wrote:
> I get a comparison error here and I see the problem but don't know how
>to fix it. It's been so long. Here's the code.
>
>#include <stdio.h>
>
>int main(int argc, char **argv)
>{
> if (argc != 2) {
> perror("argc error");
> return -1;
> }
> int i, a;
> i = a = 0;
> FILE *fp;
> if ((fp = fopen(argv[1], "r+")) == EOF) {
>
>/* right here above argv[1] is a char ** and the first parameter of fopen is
>char *. This is what I believe the problem is. How should I fix this?
>*/
> perror("fopen error");
> return -1;
> }
> for (; i != EOF; ++i) {
> fgetc(fp);
> fputc(a, fp);
> return 0;
> }
> fclose(fp);
>}
>
>


Masterful trolling! Well done, sir!

Should keep the human compilers busy for weeks.

--
"Remember when teachers, public employees, Planned Parenthood, NPR and PBS
crashed the stock market, wiped out half of our 401Ks, took trillions in
TARP money, spilled oil in the Gulf of Mexico, gave themselves billions in
bonuses, and paid no taxes? Yeah, me neither."
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      10-28-2011
Oop. Sorry for the trouble. I need to start paying attention more to my
docs. I see the comparison problem now.

Bill


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-28-2011
"Bill Cunningham" <(E-Mail Removed)> writes:
> Oop. Sorry for the trouble. I need to start paying attention more to my
> docs. I see the comparison problem now.


By all means, don't bother to tell us what the problem was. No point in
anyone else learning anything.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      10-28-2011
On Thu, 27 Oct 2011 21:08:55 -0400, Keith Thompson <(E-Mail Removed)> wrote:

> "Bill Cunningham" <(E-Mail Removed)> writes:
>> Oop. Sorry for the trouble. I need to start paying attention more to my
>> docs. I see the comparison problem now.

>
> By all means, don't bother to tell us what the problem was. No point in
> anyone else learning anything.
>


Bill implied that the error was in the line:
> if ((fp = fopen(argv[1], "r+")) == EOF) {


where, of course, he's comparing a (FILE *) to an (int)


....

Also providing some amusement is the code

> for (; i != EOF; ++i) {
> fgetc(fp);
> fputc(a, fp);
> return 0;
> }


which attempts to write a single nul byte somewhere in the file fp, and
then exit. However, unless the file was initially empty, this code
violates a constraint, since input (fgetc) is followed immediately by
output (fputc) without any intervening file-positioning operation.
(7.19.5.3, paragraph 6).
Bill seems to be off his meds again, and I have no real idea what this
code was intended to do. Even if he meant to iterate through the loop
until ((a = fgetc(fp)) == EOF), I can't guess what he intended by
writing a back to the same file.

The body of the "loop" is only executed once, which is a good thing,
otherwise it would run through thousands of iterations, at least, until
i == INT_MAX, at which point incrementing invokes (undefined? unspecified?)
behavior. On the platforms I'm most familiar with, I think that adding
one to INT_MAX is likely to result in INT_MIN, and thousands of iterations
later, i would eventually equal -1, which is the usual definition of EOF.

--
Morris Keesan -- (E-Mail Removed)
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      10-28-2011
In <4ea9f052$0$19267$(E-Mail Removed)> "Bill Cunningham" <(E-Mail Removed)> writes:

> I get a comparison error here and I see the problem but don't know how
> to fix it. It's been so long. Here's the code.


Whenever you ask for help with an error, it's best to post the exact
error you get. Just telling us "comparison error" is very vague.

> if ((fp = fopen(argv[1], "r+")) == EOF) {


You probably meant to compare to NULL here, not EOF.

> /* right here above argv[1] is a char ** and the first parameter of fopen is
> char *. This is what I believe the problem is. How should I fix this?
> */


argv is a char **, but argv[1] is a char *.

> for (; i != EOF; ++i) {
> fgetc(fp);
> fputc(a, fp);
> return 0;
> }


Why on earth is this in a loop? The return statement guarantees it will
only execute one loop iteration.

Also, "a" never gets a new value after being initialized to zero. Why?

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-28-2011
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:
>"Bill Cunningham" <(E-Mail Removed)> writes:
>> Oop. Sorry for the trouble. I need to start paying attention more to my
>> docs. I see the comparison problem now.

>
>By all means, don't bother to tell us what the problem was. No point in
>anyone else learning anything.


Agreed. (Amazing that, me & Kiki agreeing on something. Must be true, then)

--
But the Bush apologists hope that you won't remember all that. And they
also have a theory, which I've been hearing more and more - namely,
that President Obama, though not yet in office or even elected, caused the
2008 slump. You see, people were worried in advance about his future
policies, and that's what caused the economy to tank. Seriously.

(Paul Krugman - Addicted to Bush)

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      10-28-2011
On 10/28/2011 03:09 AM, John Gordon wrote:
> In<4ea9f052$0$19267$(E-Mail Removed)> "Bill Cunningham"<(E-Mail Removed)> writes:
>
>> I get a comparison error here and I see the problem but don't know how
>> to fix it. It's been so long. Here's the code.

>
> Whenever you ask for help with an error, it's best to post the exact
> error you get. Just telling us "comparison error" is very vague.
>
>> if ((fp = fopen(argv[1], "r+")) == EOF) {

>
> You probably meant to compare to NULL here, not EOF.
>
>> /* right here above argv[1] is a char ** and the first parameter of fopen is
>> char *. This is what I believe the problem is. How should I fix this?
>> */

>
> argv is a char **, but argv[1] is a char *.
>
>> for (; i != EOF; ++i) {
>> fgetc(fp);
>> fputc(a, fp);
>> return 0;
>> }

>
> Why on earth is this in a loop? The return statement guarantees it will
> only execute one loop iteration.
>
> Also, "a" never gets a new value after being initialized to zero. Why?


I don't think you're new here. So why do you believe responding to Bill
has any value? I suppose it may possibly warn any unwary reader that
Bill's code is not a helpful model, but if you need to be told that,
perhaps you need to simply read K&R or something.

 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      10-28-2011
In <j8dlno$je$(E-Mail Removed)> Mark Bluemel <(E-Mail Removed)> writes:

> I don't think you're new here. So why do you believe responding to Bill
> has any value?


Boundless optimism?

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-28-2011
Morris Keesan wrote:
> On Thu, 27 Oct 2011 21:08:55 -0400, Keith Thompson <(E-Mail Removed)>
> wrote:
>> "Bill Cunningham" <(E-Mail Removed)> writes:
>>> Oop. Sorry for the trouble. I need to start paying attention more
>>> to my docs. I see the comparison problem now.

>>
>> By all means, don't bother to tell us what the problem was. No
>> point in anyone else learning anything.
>>

>
> Bill implied that the error was in the line:
>> if ((fp = fopen(argv[1], "r+")) == EOF) {

>
> where, of course, he's comparing a (FILE *) to an (int)


I forgot NULL was needed yes.

>
> Also providing some amusement is the code
>
>> for (; i != EOF; ++i) {
>> fgetc(fp);
>> fputc(a, fp);
>> return 0;
>> }

>
> which attempts to write a single nul byte somewhere in the file fp,
> and then exit. However, unless the file was initially empty, this
> code violates a constraint, since input (fgetc) is followed
> immediately by output (fputc) without any intervening
> file-positioning operation. (7.19.5.3, paragraph 6).
> Bill seems to be off his meds again, and I have no real idea what this
> code was intended to do. Even if he meant to iterate through the loop
> until ((a = fgetc(fp)) == EOF), I can't guess what he intended by
> writing a back to the same file.
>
> The body of the "loop" is only executed once, which is a good thing,
> otherwise it would run through thousands of iterations, at least,
> until i == INT_MAX, at which point incrementing invokes (undefined?
> unspecified?) behavior. On the platforms I'm most familiar with, I
> think that adding one to INT_MAX is likely to result in INT_MIN, and
> thousands of iterations later, i would eventually equal -1, which is
> the usual definition of EOF.


This is embarrassing now that I remember how to do what I'm wanting. I
forgot it's been so long. I need two FILE * streams. One to read and one to
write. I was thinking I could do it with one. And two calls to fopen one to
strictly read and one to write. If I'm thinking right. Then I'll need a file
descriptor of 32 bits as an int and try this.

while((a=fgetc(fp)!=EOF)
fputsc(a,fp);

Sound right?

Bill


 
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
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Comparison error Jake Alucard Ruby 2 10-20-2010 01:47 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Error in Ruby text comparison? Greg Willits Ruby 6 10-27-2007 10:15 PM
Number comparison error PW ASP General 7 05-25-2004 06:05 AM



Advertisments