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


|