success! I think

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    #1

    success! I think

    I began to think about some excercises I could with files after reading
    turtorials today on arithmetic operators and confusing myself and decided to
    do this.
    Take the mv command from linux that moves and renames files read it as
    binary and write it as text. After some compiler qipes it compiled and I
    believe it did want I wanted it to.

    #include <stdio.h>

    int main(void) {
    FILE *ifp, *ofp;
    int a;
    ifp=fopen("mv", "rb");
    ofp=fopen("m"," wt");
    a=fgetc(ifp);
    if (a==EOF) {
    fclose(ifp);
    }
    fputc(a,ofp);
    if (a==EOF) {
    fclose(ofp);
    }
    }

    Because I didn't know how long the file would be I couldn't say exactly
    how many bytes I should read so fread/fwrite were out and I decided on
    fgetc/fputc. Is my code up to standards? And also can someone tell me what
    the first int parameter of fputc is and does? My references that I look at
    do not say. But I think I used it right. I guessed a 'container' for a char.

    Bill


  • Bartc

    #2
    Re: success! I think


    "Bill Cunningham" <nospam@nspam.c omwrote in message
    news:V1aMj.3202 $Ho5.2125@trndd c01...
    I began to think about some excercises I could with files after reading
    turtorials today on arithmetic operators and confusing myself and decided
    to do this.
    Take the mv command from linux that moves and renames files read it as
    binary and write it as text. After some compiler qipes it compiled and I
    believe it did want I wanted it to.
    >
    #include <stdio.h>
    >
    int main(void) {
    FILE *ifp, *ofp;
    int a;
    ifp=fopen("mv", "rb");
    ofp=fopen("m"," wt");
    a=fgetc(ifp);
    if (a==EOF) {
    fclose(ifp);
    }
    fputc(a,ofp);
    if (a==EOF) {
    fclose(ofp);
    }
    }
    >
    Because I didn't know how long the file would be I couldn't say exactly
    how many bytes I should read so fread/fwrite were out and I decided on
    fgetc/fputc. Is my code up to standards?
    Not quite.

    Try putting in a comment describing what it should do.

    Try testing it to see that it does so!

    --
    Bart


    Comment

    • Bill Cunningham

      #3
      Re: success! I think


      "Bartc" <bc@freeuk.comw rote in message
      news:1eaMj.6948 $yD2.805@text.n ews.virginmedia .com...
      Not quite.
      >
      Try putting in a comment describing what it should do.
      >
      Try testing it to see that it does so!
      >
      Well as far as testing it an EBCDIC file was written with characters so
      it seems to work. But when I changed the ofp pointer and fopen's mode to wb
      I got the same thing. So now I am doubting that I am getting what I want
      though it seemed work.

      Bill


      Comment

      • Richard

        #4
        Re: success! I think

        "Bill Cunningham" <nospam@nspam.c omwrites:
        "Bartc" <bc@freeuk.comw rote in message
        news:1eaMj.6948 $yD2.805@text.n ews.virginmedia .com...
        >Not quite.
        >>
        >Try putting in a comment describing what it should do.
        >>
        >Try testing it to see that it does so!
        >>
        Well as far as testing it an EBCDIC file was written with characters so
        it seems to work. But when I changed the ofp pointer and fopen's mode to wb
        I got the same thing. So now I am doubting that I am getting what I want
        though it seemed work.
        >
        Bill
        You are trolling aren't you?

        Comment

        • Bill Cunningham

          #5
          Re: success! I think


          "Richard" <devr_@gmail.co mwrote in message
          news:ftrcu8$tlq $2@registered.m otzarella.org.. .
          You are trolling aren't you?
          Where the hell did that come from? I am doubting that my code is doing
          what I wanted and I would like to ask for serious feedback.

          Bill


          Comment

          • Richard

            #6
            Re: success! I think

            "Bill Cunningham" <nospam@nspam.c omwrites:
            "Richard" <devr_@gmail.co mwrote in message
            news:ftrcu8$tlq $2@registered.m otzarella.org.. .
            >You are trolling aren't you?
            >
            Where the hell did that come from? I am doubting that my code is doing
            what I wanted and I would like to ask for serious feedback.
            >
            Bill
            Here's some : step through it with a debugger. That will be specific to
            your platform properly. Even a casual look at your code should tell you
            it doesn't do what you want. Stepping through with a debugger will prove
            invaluable since you clearly are a beginner in all aspects of
            programming. Rather than take you through all the issues with your code,
            try to figure it out yourself first. Go line by line and ask yourself
            what it is doing and, as importantly, why.

            Comment

            • Barry Schwarz

              #7
              Re: success! I think

              On Sat, 12 Apr 2008 21:43:17 GMT, "Bill Cunningham" <nospam@nspam.c om>
              wrote:
              I began to think about some excercises I could with files after reading
              >turtorials today on arithmetic operators and confusing myself and decided to
              >do this.
              Take the mv command from linux that moves and renames files read it as
              >binary and write it as text. After some compiler qipes it compiled and I
              >believe it did want I wanted it to.
              >
              >#include <stdio.h>
              >
              >int main(void) {
              FILE *ifp, *ofp;
              int a;
              ifp=fopen("mv", "rb");
              ofp=fopen("m"," wt");
              a=fgetc(ifp);
              How do you ever get back to read another character.
              if (a==EOF) {
              fclose(ifp);
              If you would learn to indent your code, it might make it much easier
              for you to see where you are going wrong.
              }
              fputc(a,ofp);
              if (a==EOF) {
              fclose(ofp);
              }
              }
              >
              Because I didn't know how long the file would be I couldn't say exactly
              >how many bytes I should read so fread/fwrite were out and I decided on
              Your comment makes no sense. Both fread and fwrite have methods for
              specifying how many bytes to read/write and for informing the caller
              how many bytes were actually read/written. You don't have to use them
              but to say you can't is a limitation on you and not the functions.
              >fgetc/fputc. Is my code up to standards? And also can someone tell me what
              >the first int parameter of fputc is and does? My references that I look at
              >do not say. But I think I used it right. I guessed a 'container' for a char.
              You need a better reference. Find one that describes the purpose of
              each parameter of all the standard functions. You can even find free
              copies of the draft standard (n1124 and n1256) on the web which will
              do this.


              Remove del for email

              Comment

              • Barry Schwarz

                #8
                Re: success! I think

                On Sat, 12 Apr 2008 22:23:03 GMT, "Bill Cunningham" <nospam@nspam.c om>
                wrote:
                >
                >"Bartc" <bc@freeuk.comw rote in message
                >news:1eaMj.694 8$yD2.805@text. news.virginmedi a.com...
                >Not quite.
                >>
                >Try putting in a comment describing what it should do.
                >>
                >Try testing it to see that it does so!
                >>
                Well as far as testing it an EBCDIC file was written with characters so
                >it seems to work. But when I changed the ofp pointer and fopen's mode to wb
                >I got the same thing. So now I am doubting that I am getting what I want
                >though it seemed work.
                Only for some strange definition of work. Your function doesn't
                contain any form of looping. It executes fputc and fgetc each exactly
                once. How can your function read or write more than one character?


                Remove del for email

                Comment

                • Bill Cunningham

                  #9
                  Re: success! I think

                  Only for some strange definition of work. Your function doesn't
                  contain any form of looping. It executes fputc and fgetc each exactly
                  once. How can your function read or write more than one character?
                  >
                  I'm not exactly sure how to use fgetc and fputc. This is the first time
                  I've put them into code so I'm a little confused on where in the loops to
                  put them. I've never really used debuggers though I have gdb. I don't think
                  it takes more than a keaner eye than mine to see the problems though.

                  Bill


                  Comment

                  • Bill Cunningham

                    #10
                    Re: success! I think


                    "Barry Schwarz" <schwarzb@dqel. comwrote in message
                    news:j34304dfb2 i25tjq6teflmqol nh58qfuin@4ax.c om...
                    > Because I didn't know how long the file would be I couldn't say
                    >exactly
                    >>how many bytes I should read so fread/fwrite were out and I decided on
                    >
                    Your comment makes no sense. Both fread and fwrite have methods for
                    specifying how many bytes to read/write and for informing the caller
                    how many bytes were actually read/written. You don't have to use them
                    but to say you can't is a limitation on you and not the functions.
                    Exactly. Fread and fwrite require a buffer size and number of elements.
                    I just wanted to read the entire file without having to enter an exact size
                    so I don't think fwrite and fread would be the choice here.

                    Bill


                    Comment

                    • Bartc

                      #11
                      Re: success! I think


                      "Bill Cunningham" <nospam@nspam.c omwrote in message
                      news:gtvMj.4699 $bx3.1392@trndd c02...
                      >
                      "Barry Schwarz" <schwarzb@dqel. comwrote in message
                      news:j34304dfb2 i25tjq6teflmqol nh58qfuin@4ax.c om...
                      >> Because I didn't know how long the file would be I couldn't say
                      >>exactly
                      >>>how many bytes I should read so fread/fwrite were out and I decided on
                      >>
                      >Your comment makes no sense. Both fread and fwrite have methods for
                      >specifying how many bytes to read/write and for informing the caller
                      >how many bytes were actually read/written. You don't have to use them
                      >but to say you can't is a limitation on you and not the functions.
                      >
                      Exactly. Fread and fwrite require a buffer size and number of elements.
                      I just wanted to read the entire file without having to enter an exact
                      size so I don't think fwrite and fread would be the choice here.
                      In your original post you gave the impression the code worked and you wanted
                      comments on the coding standard.

                      Clearly you hadn't tested the code, unless you tested with a file containing
                      a single character.

                      The code below does a file copy byte by byte using fgetc/fputc, using binary
                      mode, of any type of file.

                      If you want to translate between text files, mixing binary and text modes on
                      input and output, that might give some strange effects.

                      If you want to translate a binary executable file as you indicated in your
                      post, into text, that is fairly meaningless unless you're talking about some
                      sort of dump or disassembly, but in that case I'd advise to forget that for
                      now.

                      /* Copy input file to output file */
                      #include <stdio.h>
                      #include <stdlib.h>

                      #define inputfile "mv"
                      #define outputfile "m"

                      int main(void) {

                      FILE *ifp, *ofp;
                      int a;

                      ifp=fopen(input file,"rb");

                      if (ifp==0){
                      printf("Can't open input file %s\n",inputfile );
                      exit (0);
                      }

                      ofp=fopen(outpu tfile,"wb");
                      if (ofp==0){
                      printf("Can't open output file %s\n",outputfil e);
                      fclose(ifp);
                      exit (0);
                      }

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

                      fclose(ifp);
                      fclose(ofp);

                      }

                      --
                      Bart


                      Comment

                      • Bill Cunningham

                        #12
                        Re: success! I think


                        "Bartc" <bc@freeuk.comw rote in message
                        news:ShwMj.7536 $yD2.2024@text. news.virginmedi a.com...
                        >
                        "Bill Cunningham" <nospam@nspam.c omwrote in message
                        news:gtvMj.4699 $bx3.1392@trndd c02...
                        >>
                        >"Barry Schwarz" <schwarzb@dqel. comwrote in message
                        >news:j34304dfb 2i25tjq6teflmqo lnh58qfuin@4ax. com...
                        >>> Because I didn't know how long the file would be I couldn't say
                        >>>exactly
                        >>>>how many bytes I should read so fread/fwrite were out and I decided on
                        >>>
                        >>Your comment makes no sense. Both fread and fwrite have methods for
                        >>specifying how many bytes to read/write and for informing the caller
                        >>how many bytes were actually read/written. You don't have to use them
                        >>but to say you can't is a limitation on you and not the functions.
                        >>
                        > Exactly. Fread and fwrite require a buffer size and number of
                        >elements. I just wanted to read the entire file without having to enter
                        >an exact size so I don't think fwrite and fread would be the choice here.
                        >
                        In your original post you gave the impression the code worked and you
                        wanted comments on the coding standard.
                        >
                        Clearly you hadn't tested the code, unless you tested with a file
                        containing a single character.
                        That's what I must've done.
                        The code below does a file copy byte by byte using fgetc/fputc, using
                        binary mode, of any type of file.
                        >
                        If you want to translate between text files, mixing binary and text modes
                        on input and output, that might give some strange effects.
                        >
                        If you want to translate a binary executable file as you indicated in your
                        post, into text, that is fairly meaningless unless you're talking about
                        some sort of dump or disassembly, but in that case I'd advise to forget
                        that for now.
                        OK
                        /* Copy input file to output file */
                        #include <stdio.h>
                        #include <stdlib.h>
                        >
                        #define inputfile "mv"
                        #define outputfile "m"
                        >
                        int main(void) {
                        >
                        FILE *ifp, *ofp;
                        int a;
                        >
                        ifp=fopen(input file,"rb");
                        >
                        if (ifp==0){
                        printf("Can't open input file %s\n",inputfile );
                        exit (0);
                        }
                        >
                        ofp=fopen(outpu tfile,"wb");
                        if (ofp==0){
                        printf("Can't open output file %s\n",outputfil e);
                        fclose(ifp);
                        exit (0);
                        }
                        >
                        while ((a=fgetc(ifp)) !=EOF)
                        fputc(a,ofp);
                        >
                        fclose(ifp);
                        fclose(ofp);
                        >
                        }
                        >
                        I appreciate your patience. When I first looked at C for some reason I
                        became transfixed. I know Basic pretty well but I would like to learn C very
                        much. Some times I sound confused because much of the time I am. I have
                        lived in the same town all my life and I pretty much gave up for the most
                        part on driving because I would start out somewhere and end up on the
                        opposite side of town. This is the confusion I have to deal with with drugs
                        that stop my panic attacks.

                        So I'm not just going to catch onto a language like C. But I will
                        continue to try but I think I may have a hard road to hoe. Thanks again.

                        Bill


                        Comment

                        • lawrence.jones@siemens.com

                          #13
                          Re: success! I think

                          Bill Cunningham <nospam@nspam.c omwrote:
                          >
                          Exactly. Fread and fwrite require a buffer size and number of elements.
                          I just wanted to read the entire file without having to enter an exact size
                          so I don't think fwrite and fread would be the choice here.
                          That's because you're not thinking enough. If you just want to read
                          bytes, use an element size of 1 and make the number of elements the
                          length of your buffer. fread() returns the number of elements it
                          actually read, which is what you need to pass to fwrite() to write the
                          chunk of data that you just read. Just do that in a loop to read and
                          writes successive chunks of the file until there's nothing left to read
                          and you're done.

                          -Larry Jones

                          I think grown-ups just ACT like they know what they're doing. -- Calvin

                          Comment

                          • Bill Cunningham

                            #14
                            Re: success! I think


                            <lawrence.jones @siemens.comwro te in message
                            news:gpiad5-tg3.ln1@jones.h omeip.net...
                            That's because you're not thinking enough. If you just want to read
                            bytes, use an element size of 1 and make the number of elements the
                            length of your buffer. fread() returns the number of elements it
                            actually read, which is what you need to pass to fwrite() to write the
                            chunk of data that you just read. Just do that in a loop to read and
                            writes successive chunks of the file until there's nothing left to read
                            and you're done.
                            >
                            Hum. There is sizeof. This might just work.

                            fread(buff,size of(int),1,fp);

                            I could try that. I just bet it would work to.

                            Bill


                            Comment

                            • Barry Schwarz

                              #15
                              Re: success! I think

                              On Mon, 14 Apr 2008 01:41:05 GMT, "Bill Cunningham" <nospam@nspam.c om>
                              wrote:
                              >
                              ><lawrence.jone s@siemens.comwr ote in message
                              >news:gpiad5-tg3.ln1@jones.h omeip.net...
                              >That's because you're not thinking enough. If you just want to read
                              >bytes, use an element size of 1 and make the number of elements the
                              >length of your buffer. fread() returns the number of elements it
                              >actually read, which is what you need to pass to fwrite() to write the
                              >chunk of data that you just read. Just do that in a loop to read and
                              >writes successive chunks of the file until there's nothing left to read
                              >and you're done.
                              >>
                              Hum. There is sizeof. This might just work.
                              >
                              >fread(buff,siz eof(int),1,fp);
                              >
                              >I could try that. I just bet it would work to.
                              What makes you think the size of an integer has any significance to
                              the code you presented?


                              Remove del for email

                              Comment

                              Working...