Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: program bug
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 36 Topic 26044 of 26977
Post > Topic >>

Re: program bug

by Robert Gamble <rgamble99@[EMAIL PROTECTED] > May 2, 2008 at 03:05 PM

On May 2, 5:40 pm, "Bill Cunningham" <nos...@[EMAIL PROTECTED]
> wrote:
>     I have looked this program up and down and I don't see what's wrong
with
> it. But it always breaks and gives me an error "mode error" no matter
which
> mode binary or text I choose. This simple program is supposed to take as
> argv[1] a "b" or "t" for binary or text. It's not taking anything.
>
> #include <stdio.h>
>
> int main(int argc, char **argv) {
>  char *b;
>  int a;
>  FILE *ifp,*ofp;
>  if (argc!=4) {
>  fprintf(stderr,"usage error\n");
>  return -1;

Not a ****table return value from main.

>  }
>  if (argv[1]=="b") {

This isn't doing what you think it is.  This is comparing the address
of first program argument to the address of the string literal "b"
which will always be different.  Either use (strcmp(argv[1], "b") ==
0) or say:

if (argv[1][0] == 'b') {

which will compare the first letter of the first argument to the
character constant b.

>  b="rb";
>  }
>  if (argv[1]=="t") {
>  b="rt";
>  }
>  if (argv[1]!="t"||argv[1]!="b") {
>  fprintf(stderr,"mode error\n");
>  return -1;
>  }
>  if ((ifp=fopen(argv[2],b))==0) {
>  fprintf(stderr,"open error i\n");
>  return -1;
>  }
>  if ((ofp=fopen(argv[3],b))==0) {
>  fprintf(stderr,"open error o\n");
>  return -1;
>  }
>  while(a!=EOF)

a is not initialized, referencing its value invokes undefined
behavior.

>  a=fgetc(ifp);
>  fputc(a,ofp);

I think you want braces around these statements, yes?

Try (not tested):

while ( (a = fgetc(ifp)) != EOF && fputc(a, ofp) != EOF )
  ;

>  printf("done\n");

puts("done"); would work equally well.

>  return 0;}
>
>     Is anyone good enough to glance at this and see what's wrong?
>
> Bill

--
Robert Gamble
 




 36 Posts in Topic:
program bug
"Bill Cunningham&quo  2008-05-02 21:40:19 
Re: program bug
"Bill Cunningham&quo  2008-05-02 21:53:34 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:08:03 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:05:58 
Re: program bug
"Bill Cunningham&quo  2008-05-02 22:16:27 
Re: program bug
CBFalconer <cbfalconer  2008-05-02 18:47:28 
Re: program bug
"Bill Cunningham&quo  2008-05-02 22:21:04 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:28:04 
Re: program bug
"Bill Cunningham&quo  2008-05-03 20:42:30 
Re: program bug
Keith Thompson <kst-u@  2008-05-03 14:48:48 
Re: program bug
"Bill Cunningham&quo  2008-05-03 22:58:24 
Re: program bug
Keith Thompson <kst-u@  2008-05-03 18:08:44 
Re: program bug
"Default User"   2008-05-04 03:20:33 
Re: program bug
Keith Thompson <kst-u@  2008-05-03 21:17:13 
Re: program bug
"Default User"   2008-05-04 19:03:07 
Re: program bug
vippstar@[EMAIL PROTECTED  2008-05-03 16:21:48 
Re: program bug
CBFalconer <cbfalconer  2008-05-03 20:29:01 
Re: program bug
Joe Wright <joewwright  2008-05-02 18:25:33 
Re: program bug
Ian Collins <ian-news@  2008-05-03 10:28:17 
Re: program bug
"Bill Cunningham&quo  2008-05-02 22:36:04 
Re: program bug
Robert Gamble <rgamble  2008-05-02 15:36:31 
Re: program bug
Ian Collins <ian-news@  2008-05-03 11:31:53 
Re: program bug
Joe Wright <joewwright  2008-05-02 20:18:17 
Re: program bug
Ian Collins <ian-news@  2008-05-03 12:31:09 
Re: program bug
"Bill Cunningham&quo  2008-05-02 23:15:17 
Re: program bug
Keith Thompson <kst-u@  2008-05-02 23:56:46 
Re: program bug
Richard Heathfield <rj  2008-05-03 07:04:24 
Re: program bug
CBFalconer <cbfalconer  2008-05-02 18:50:15 
Re: program bug
user923005 <dcorbit@[E  2008-05-02 21:25:53 
Re: program bug
"Bill Cunningham&quo  2008-05-03 15:52:27 
Re: program bug
vippstar@[EMAIL PROTECTED  2008-05-03 09:01:52 
Re: program bug
Eligiusz Narutowicz<el  2008-05-04 08:41:56 
Re: program bug
"Bill Cunningham&quo  2008-05-04 21:25:27 
Re: program bug
Barry Schwarz <schwarz  2008-05-04 15:20:34 
Re: program bug
"Bill Cunningham&quo  2008-05-06 02:30:36 
Re: program bug
Nick Keighley <nick_ke  2008-05-06 02:28:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:35:34 CDT 2008.