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 29 of 36 Topic 26044 of 26955
Post > Topic >>

Re: program bug

by user923005 <dcorbit@[EMAIL PROTECTED] > May 2, 2008 at 09:25 PM

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;
}
 




 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 Thu Jul 24 2:15:14 CDT 2008.