success! I think

Discussion in 'C Programming' started by Bill Cunningham, Apr 12, 2008.

  1. 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
     
    Bill Cunningham, Apr 12, 2008
    #1
    1. Advertising

  2. Bill Cunningham

    Bartc Guest

    "Bill Cunningham" <> wrote in message
    news:V1aMj.3202$Ho5.2125@trnddc01...
    > 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
     
    Bartc, Apr 12, 2008
    #2
    1. Advertising

  3. "Bartc" <> wrote in message
    news:1eaMj.6948$...
    > 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
     
    Bill Cunningham, Apr 12, 2008
    #3
  4. Bill Cunningham

    Richard Guest

    "Bill Cunningham" <> writes:

    > "Bartc" <> wrote in message
    > news:1eaMj.6948$...
    >> 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?
     
    Richard, Apr 12, 2008
    #4
  5. "Richard" <> wrote in message
    news:ftrcu8$tlq$...
    > 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
     
    Bill Cunningham, Apr 12, 2008
    #5
  6. Bill Cunningham

    Richard Guest

    "Bill Cunningham" <> writes:

    > "Richard" <> wrote in message
    > news:ftrcu8$tlq$...
    >> 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.
     
    Richard, Apr 12, 2008
    #6
  7. On Sat, 12 Apr 2008 21:43:17 GMT, "Bill Cunningham" <>
    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
     
    Barry Schwarz, Apr 13, 2008
    #7
  8. On Sat, 12 Apr 2008 22:23:03 GMT, "Bill Cunningham" <>
    wrote:

    >
    >"Bartc" <> wrote in message
    >news:1eaMj.6948$...
    >> 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
     
    Barry Schwarz, Apr 13, 2008
    #8
  9. > 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
     
    Bill Cunningham, Apr 13, 2008
    #9
  10. "Barry Schwarz" <> wrote in message
    news:...
    >> 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
     
    Bill Cunningham, Apr 13, 2008
    #10
  11. Bill Cunningham

    Bartc Guest

    "Bill Cunningham" <> wrote in message
    news:gtvMj.4699$bx3.1392@trnddc02...
    >
    > "Barry Schwarz" <> wrote in message
    > news:...
    >>> 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(inputfile,"rb");

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

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

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

    fclose(ifp);
    fclose(ofp);

    }

    --
    Bart
     
    Bartc, Apr 14, 2008
    #11
  12. "Bartc" <> wrote in message
    news:ShwMj.7536$...
    >
    > "Bill Cunningham" <> wrote in message
    > news:gtvMj.4699$bx3.1392@trnddc02...
    >>
    >> "Barry Schwarz" <> wrote in message
    >> news:...
    >>>> 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(inputfile,"rb");
    >
    > if (ifp==0){
    > printf("Can't open input file %s\n",inputfile);
    > exit (0);
    > }
    >
    > ofp=fopen(outputfile,"wb");
    > if (ofp==0){
    > printf("Can't open output file %s\n",outputfile);
    > 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
     
    Bill Cunningham, Apr 14, 2008
    #12
  13. Bill Cunningham

    Guest

    Bill Cunningham <> wrote:
    >
    > 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
     
    , Apr 14, 2008
    #13
  14. <> wrote in message
    news:...
    > 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,sizeof(int),1,fp);

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

    Bill
     
    Bill Cunningham, Apr 14, 2008
    #14
  15. On Mon, 14 Apr 2008 01:41:05 GMT, "Bill Cunningham" <>
    wrote:

    >
    ><> wrote in message
    >news:...
    >> 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,sizeof(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
     
    Barry Schwarz, Apr 14, 2008
    #15
  16. On 12 Apr, 22:43, "Bill Cunningham" <> 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);
    >  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.


    you posted an almost identical problem in february. I gave
    you a general way to copy a file. use google "keighley cunningham"
    to find it.

    Either learn this stuff or give up learning C.


    --
    Nick Keighley
     
    Nick Keighley, Apr 14, 2008
    #16
  17. Bill Cunningham

    Richard Guest

    "Bill Cunningham" <> writes:

    > <> wrote in message
    > news:...
    >> 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,sizeof(int),1,fp);
    >
    > I could try that. I just bet it would work to.
    >
    > Bill


    Learn to read a man page or a C book or, and I hate to say this, give
    up. Personally I think you are trolling.

    But if you're not, read your program as a cpu would generally do it -
    line by line and statement by statement. Your code and your thinking
    seem to bear almost no resemblance to the problem you want to solve.
     
    Richard, Apr 14, 2008
    #17
  18. you posted an almost identical problem in february. I gave
    you a general way to copy a file. use google "keighley cunningham"
    to find it.

    Either learn this stuff or give up learning C.

    Nick I still have a copy of the file you posted and I am going to keep
    it. I think I need to learn more about loops like while. You used an example
    of do. Your code was very thorough and precise. I haven't got to do in the
    tutorials but I will continue reading and studing your code. Don't think
    your post was in vain.

    Bill


    --
    Nick Keighley
     
    Bill Cunningham, Apr 14, 2008
    #18
  19. > What makes you think the size of an integer has any significance to
    > the code you presented?


    I was addressing Lawrence's post concerning fread. It really didn't have
    much to do with using fgetc and fputc or the first code I posted.

    Bill
     
    Bill Cunningham, Apr 14, 2008
    #19
  20. On 14 Apr, 22:32, "Bill Cunningham" <> wrote:
    > you posted an almost identical problem in february. I gave
    > you a general way to copy a file. use google "keighley cunningham"
    > to find it.
    >
    > Either learn this stuff or give up learning C.
    >
    >     Nick I still have a copy of the file you posted and I am going to keep
    > it. I think I need to learn more about loops like while. You used an example
    > of do. Your code was very thorough and precise. I haven't got to do in the
    > tutorials but I will continue reading and studing your code. Don't think
    > your post was in vain.


    your current problem seems almost identical to one I
    gave a solution for. The only difference is the previous one
    used fread/fwrite. I cannot comprehend how you can read my code
    and then post code that tries to copy a file ***without using a
    loop construct***


    --
    Nick Keighley
     
    Nick Keighley, Apr 15, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MCDONAMW
    Replies:
    4
    Views:
    594
    MCDONAMW
    Sep 28, 2004
  2. DC

    ASP.NET success stories?

    DC, Dec 2, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    946
    Jeff Evans
    Dec 4, 2004
  3. Replies:
    0
    Views:
    607
  4. Replies:
    1
    Views:
    870
    Jack Klein
    Apr 13, 2005
  5. David Mark
    Replies:
    17
    Views:
    241
    David Mark
    Mar 23, 2010
Loading...

Share This Page