On May 2, 2:40=A0pm, "Bill Cunningham" <nos...@[EMAIL PROTECTED]
> wrote:
> =A0 =A0 I have looked this program up and down and I don't see what's
wron=
g with
> it. But it always breaks and gives me an error "mode error" no matter
whic=
h
> 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) {
> =A0char *b;
> =A0int a;
> =A0FILE *ifp,*ofp;
> =A0if (argc!=3D4) {
> =A0fprintf(stderr,"usage error\n");
> =A0return -1;
> =A0}
> =A0if (argv[1]=3D=3D"b") {
> =A0b=3D"rb";
> =A0}
> =A0if (argv[1]=3D=3D"t") {
> =A0b=3D"rt";
> =A0}
> =A0if (argv[1]!=3D"t"||argv[1]!=3D"b") {
> =A0fprintf(stderr,"mode error\n");
> =A0return -1;
> =A0}
> =A0if ((ifp=3Dfopen(argv[2],b))=3D=3D0) {
> =A0fprintf(stderr,"open error i\n");
> =A0return -1;
> =A0}
> =A0if ((ofp=3Dfopen(argv[3],b))=3D=3D0) {
> =A0fprintf(stderr,"open error o\n");
> =A0return -1;
> =A0}
> =A0while(a!=3DEOF)
> =A0a=3Dfgetc(ifp);
> =A0fputc(a,ofp);
> =A0printf("done\n");
> =A0return 0;}
>
> =A0 =A0 Is anyone good enough to glance at this and see what's wrong?
Too many things for a glance. It requires a perusal.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char *imode =3D 0;
char *omode =3D 0;
char option;
int ch =3D 0;
FILE *ifp,
*ofp;
if (argc !=3D 4) {
fprintf(stderr, "usage error\n");
return EXIT_FAILURE;
}
option =3D *argv[1];
if (option =3D=3D 'b') {
imode =3D "rb";
omode =3D "wb";
}
if (option =3D=3D 't') {
/* This may or may not do something useful on your system */
/* The 't' in the mode is not ****table, but is a fairly */
/* popular extension for text mode. */
imode =3D "rt";
omode =3D "wt";
}
if (option !=3D 't' && option !=3D 'b') {
fprintf(stderr, "mode error\n");
return EXIT_FAILURE;
}
if ((ifp =3D fopen(argv[2], imode)) =3D=3D 0) {
fprintf(stderr, "open error i\n");
return EXIT_FAILURE;
}
if ((ofp =3D fopen(argv[3], omode)) =3D=3D 0) {
fprintf(stderr, "open error o\n");
return EXIT_FAILURE;
}
/* I guess that you want to write what you read, hence the {} */
do {
ch =3D fgetc(ifp);
if (ch !=3D EOF)
fputc(ch, ofp);
} while (ch !=3D EOF);
fclose(ifp);
fclose(ofp);
printf("done\n");
return 0;
}


|