Re: Question about fopen, fput, fclose?

Discussion in 'C Programming' started by prama, Aug 26, 2003.

  1. prama

    prama Guest

    I think this will help you.

    Rather than typing " while(letter!='\r');" in the program,code it as
    while(letter!=10);
    We are comparing with 10 since the ascii value of return is 10.



    (RiRyoKu-Nightmare) wrote in message news:<>...
    > The following is source from a novice program that should open a file
    > and allow me to write to the file until <RETURN> is pressed but it
    > does not work on my
    > Redhat Linux 7.2, gcc, emacs editor:
    > +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
    > /* fputc.c */
    > #include <stdio.h>
    > main()
    > {
    > FILE *fp;
    > char letter;
    > puts("Type something and <PRESS ENTER> when finished to save.\n");
    > if((fp = fopen("MYFILE.TEXT","w"))==NULL)
    > {
    > puts("Cannot open the file\n");
    > exit();
    > }
    > do
    > {
    > letter=getchar();
    > fputc(letter, fp);
    > }
    > while(letter!='\r');
    > fclose(fp);
    > return(0);
    > }
    > +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
    >
    >
    > My question is how do i get "while(letter!='\r'):" to work, it just
    > newlines instead of ending the do..while loop and my file doesn't
    > write correctly. I hope you can show me some tips or point me to the
    > write Documentation.
    > R.Thomas
    prama, Aug 26, 2003
    #1
    1. Advertising

  2. prama

    Joe Wright Guest

    prama wrote:
    >
    > (RiRyoKu-Nightmare) wrote in message news:<>...
    > > The following is source from a novice program that should open a file
    > > and allow me to write to the file until <RETURN> is pressed but it
    > > does not work on my
    > > Redhat Linux 7.2, gcc, emacs editor:
    > > +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
    > > /* fputc.c */
    > > #include <stdio.h>
    > > main()
    > > {
    > > FILE *fp;
    > > char letter;
    > > puts("Type something and <PRESS ENTER> when finished to save.\n");
    > > if((fp = fopen("MYFILE.TEXT","w"))==NULL)
    > > {
    > > puts("Cannot open the file\n");
    > > exit();
    > > }
    > > do
    > > {
    > > letter=getchar();
    > > fputc(letter, fp);
    > > }
    > > while(letter!='\r');
    > > fclose(fp);
    > > return(0);
    > > }
    > > +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
    > >
    > >
    > > My question is how do i get "while(letter!='\r'):" to work, it just
    > > newlines instead of ending the do..while loop and my file doesn't
    > > write correctly. I hope you can show me some tips or point me to the
    > > write Documentation.
    > > R.Thomas


    > I think this will help you.
    >
    > Rather than typing " while(letter!='\r');" in the program,code it as
    > while(letter!=10);
    > We are comparing with 10 since the ascii value of return is 10.
    >


    No. The effect of the ENTER key is to place '\n' (newline) on the input
    stream. There is no requirement that '\n' == 10, although it is in
    ASCII.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Aug 26, 2003
    #2
    1. Advertising

  3. prama

    goose Guest

    (prama) wrote in message news:<>...
    > I think this will help you.
    >
    > Rather than typing " while(letter!='\r');" in the program,code it as
    > while(letter!=10);
    > We are comparing with 10 since the ascii value of return is 10.
    >


    no no no !!! thats just wanton cruelty to the number 10. theres
    no need to make your code work only on *ascii* systems, instead
    try
    while (letter!='\r' || letter!='\n')


    hth
    goose,
    the whole world is *not* ascii !!!
    goose, Aug 26, 2003
    #3
  4. prama

    John Bode Guest

    (prama) wrote in message news:<>...
    > I think this will help you.
    >
    > Rather than typing " while(letter!='\r');" in the program,code it as
    > while(letter!=10);
    > We are comparing with 10 since the ascii value of return is 10.
    >


    What if we're using EBCDIC instead of ASCII? Or Unicode?

    Using the character constant is the Right Thing to do, because the
    right comparison will be made regardless of whether we're using ASCII
    or not. It also has the advantage of being self-documenting. How
    many people are going to remember that 10 means '\r' in ASCII? It's
    just that in this particular case, it's the wrong character constant
    being used.

    >
    >
    > (RiRyoKu-Nightmare) wrote in message news:<>...
    > > The following is source from a novice program that should open a file
    > > and allow me to write to the file until <RETURN> is pressed but it
    > > does not work on my
    > > Redhat Linux 7.2, gcc, emacs editor:
    > > +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
    > > /* fputc.c */
    > > #include <stdio.h>
    > > main()
    > > {
    > > FILE *fp;
    > > char letter;
    > > puts("Type something and <PRESS ENTER> when finished to save.\n");
    > > if((fp = fopen("MYFILE.TEXT","w"))==NULL)
    > > {
    > > puts("Cannot open the file\n");
    > > exit();
    > > }
    > > do
    > > {
    > > letter=getchar();
    > > fputc(letter, fp);
    > > }
    > > while(letter!='\r');


    Try '\n' instead of '\r'. In the context of C stdio, '\n' is the
    end-of-line marker, whereas '\r' is simply a carriage return.

    > > fclose(fp);
    > > return(0);
    > > }
    > > +_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_
    > >
    > >
    > > My question is how do i get "while(letter!='\r'):" to work, it just
    > > newlines instead of ending the do..while loop and my file doesn't
    > > write correctly. I hope you can show me some tips or point me to the
    > > write Documentation.
    > > R.Thomas
    John Bode, Aug 26, 2003
    #4
  5. John Bode <> scribbled the following:
    > (prama) wrote in message news:<>...
    >> I think this will help you.
    >>
    >> Rather than typing " while(letter!='\r');" in the program,code it as
    >> while(letter!=10);
    >> We are comparing with 10 since the ascii value of return is 10.


    > What if we're using EBCDIC instead of ASCII? Or Unicode?


    Unicode overlaps ASCII on codes 0x0000 through 0x007F, and overlaps
    ISO-8859-1 on codes 0x0000 through 0x00FF.

    > Using the character constant is the Right Thing to do, because the
    > right comparison will be made regardless of whether we're using ASCII
    > or not. It also has the advantage of being self-documenting. How
    > many people are going to remember that 10 means '\r' in ASCII? It's
    > just that in this particular case, it's the wrong character constant
    > being used.


    This much is true.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "A friend of mine is into Voodoo Acupuncture. You don't have to go into her
    office. You'll just be walking down the street and... ohh, that's much better!"
    - Stephen Wright
    Joona I Palaste, Aug 26, 2003
    #5
  6. prama

    Jack Klein Guest

    On 26 Aug 2003 03:12:32 -0700, (prama) wrote in
    comp.lang.c:

    > I think this will help you.
    >
    > Rather than typing " while(letter!='\r');" in the program,code it as
    > while(letter!=10);
    > We are comparing with 10 since the ascii value of return is 10.


    The ASCII code (there is no such thing as "ascii" code) for the
    carriage return is 13, not 10. But that has nothing at all to do with
    what a C program sees as the end of a text line. That is '\n', and
    the numeric value is implementation-defined.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Aug 28, 2003
    #6
    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. Trying_Harder

    fgets,fopen, fclose

    Trying_Harder, Aug 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    682
    John Bode
    Sep 3, 2003
  2. Peter Shaggy Haywood

    Re: Question about fopen, fput, fclose?

    Peter Shaggy Haywood, Aug 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    393
    Peter Shaggy Haywood
    Aug 30, 2003
  3. lihua
    Replies:
    19
    Views:
    922
    CBFalconer
    Jul 7, 2005
  4. David Mathog

    fclose then fopen equivalent for stdout?

    David Mathog, Oct 25, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,005
    Jordan Abel
    Oct 27, 2006
  5. kathy

    fopen and fclose?

    kathy, Feb 3, 2006, in forum: C++
    Replies:
    17
    Views:
    633
    Default User
    Feb 15, 2006
Loading...

Share This Page