Re: program bug



Bill Cunningham 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;
}
if (argv[1]=="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=fgetc(ifp);
fputc(a,ofp);
printf("done\n");
return 0;}

Is anyone good enough to glance at this and see what's wrong?

Someone stole the whitespace?

Why are you comparing characters with string literals? Surly your
compiler gave you some warnings?

--
Ian Collins.
.



Relevant Pages

  • Re: Just a bit of silliness
    ... No matter what type x is, ...
    (comp.lang.c)
  • Re: type names
    ... or long placed after char, int or double. ... matter is key to keep code readable and maintainable. ... and sensible advice when writing c code. ...
    (comp.std.c)
  • Re: program bug
    ... But it always breaks and gives me an error "mode error" no matter which ... int main(int argc, char **argv) { ... This is comparing the address ...
    (comp.lang.c)
  • Re: why getchar is not executed??
    ... donot have the chance to input a char to z; ... getcharreturns an int, but you assign its result to a char. ... probably doesn't matter much here, since you're about to terminate the ... You call exit() with no arguments. ...
    (comp.lang.c)
  • Re: Just a bit of silliness
    ... No matter what type x is, ...
    (comp.lang.c)