Reversing Input

Discussion in 'C Programming' started by jjmacias2007@gmail.com, Mar 29, 2006.

  1. Guest

    Hey I'm writing be cause I am requiring some help on this program I
    need to write. I was able to look at some sample code done back in
    Dec. somewhat similar to what I need to do. The thing with that code
    is that it reads only one line of input data reverses it and also adds
    on the original line. I tried using while(gets(line)!=NULL) to make
    the program read all the lines of input data but it didn't work. The
    program requires that I input data from stdin out put it in the same
    exact pattern but reversed and give an ending number of lines and
    length of file. Does any body have any suggestions?
     
    , Mar 29, 2006
    #1
    1. Advertising

  2. opined:

    > Hey I'm writing be cause I am requiring some help on this program I
    > need to write. I was able to look at some sample code done back in
    > Dec. somewhat similar to what I need to do. The thing with that code
    > is that it reads only one line of input data reverses it and also
    > adds
    > on the original line. I tried using while(gets(line)!=NULL) to make
    > the program read all the lines of input data but it didn't work. The
    > program requires that I input data from stdin out put it in the same
    > exact pattern but reversed and give an ending number of lines and
    > length of file. Does any body have any suggestions?


    Yes.

    Since you're already using Google to access Usenet, why not also use
    its fine search functionality, and search this very newsgroup. All of
    the questions you have asked above have been answered, and discussed
    in detail here in the past month or two.

    Reading:

    <http://cfaj.freeshell.org/google/>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

    before posting here again, is also a /very/ good idea.

    --
    BR, Vladimir

    An envious girl named McMeanus
    Was jealous of her lover's big penis.
    It was small consolation
    That the rest of the nation
    Of women were with her in weeness.
     
    Vladimir S. Oka, Mar 29, 2006
    #2
    1. Advertising

  3. Pedro Graca Guest

    wrote:
    > [...]
    > I tried using while(gets(line)!=NULL) to make


    Don't use gets().
    Never use gets().
    gets() is evil.
    Never use gets(). /* oh! I'm repeating myself */

    If your teacher tells you to use gets(), respectfully tell her she's
    wrong and fgets() should be used instead.

    > Does any body have any suggestions?


    Do *not* use gets()

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
     
    Pedro Graca, Mar 29, 2006
    #3
  4. sonu Guest

    Then why gets() is there?









    Pedro Graca wrote:
    > wrote:
    > > [...]
    > > I tried using while(gets(line)!=NULL) to make

    >
    > Don't use gets().
    > Never use gets().
    > gets() is evil.
    > Never use gets(). /* oh! I'm repeating myself */
    >
    > If your teacher tells you to use gets(), respectfully tell her she's
    > wrong and fgets() should be used instead.
    >
    > > Does any body have any suggestions?

    >
    > Do *not* use gets()
    >
    > --
    > If you're posting through Google read <http://cfaj.freeshell.org/google>
     
    sonu, Mar 29, 2006
    #4
  5. sonu wrote:

    Don't top post. Corrected here...

    > Pedro Graca wrote:
    > > wrote:
    > > > [...]
    > > > I tried using while(gets(line)!=NULL) to make

    > >
    > > Don't use gets().
    > > Never use gets().
    > > gets() is evil.
    > > Never use gets(). /* oh! I'm repeating myself */
    > >
    > > If your teacher tells you to use gets(), respectfully tell her she's
    > > wrong and fgets() should be used instead.
    > >
    > > > Does any body have any suggestions?

    > >
    > > Do *not* use gets()

    >
    > Then why gets() is there?


    Presumably not to break (inherently unsafe, if it uses `gets()`) legacy
    code. There have been many, and long, discussions about that recently.
    Search Google Groups if you're really interested.

    --
    BR, Vladimir
     
    Vladimir S. Oka, Mar 29, 2006
    #5
  6. On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
    <> wrote:

    >Then why gets() is there?


    Because when the language was first designed, it was found useful by
    the *highly expert* people using it, in the *tightly controlled*
    environment they used.

    Nowadays its there to avoid breaking legacy code, even though its
    impossible to use safely by anyone other than an expert operating in a
    completely controlled environment.

    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Mar 29, 2006
    #6
  7. CBFalconer Guest

    sonu wrote:
    >
    > Then why gets() is there?


    Don't top-post. gets is present for the same reason that dogs
    leave smelly messes that stick to the bottom of your shoes, and can
    be tracked about.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Mar 29, 2006
    #7
  8. Flash Gordon Guest

    CBFalconer wrote:
    > sonu wrote:
    >> Then why gets() is there?

    >
    > Don't top-post. gets is present for the same reason that dogs
    > leave smelly messes that stick to the bottom of your shoes, and can
    > be tracked about.


    That sonu asked this also shows why gets should be removed from the
    standard.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Mar 29, 2006
    #8
  9. Mark McIntyre <> writes:
    > On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
    > <> wrote:
    >>Then why gets() is there?

    >
    > Because when the language was first designed, it was found useful by
    > the *highly expert* people using it, in the *tightly controlled*
    > environment they used.


    I'm not convinced that the old environments were that tightly
    controlled. I suspect people back then were just more willing to
    accept the risk of a buffer overflow, and didn't care as much about
    the possibility of a user typing (or a file containing) hundreds of
    characters on a single line. In the absence of networking, the
    consequences of a buffer overflow were unlikely to be all that severe.

    In any case, it's universally agreed that gets() should not be used
    (except possibly in some very obscure circumstances), and ther seems
    to be a strong majority in favor of removing it from the standard.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 29, 2006
    #9
  10. On Wed, 29 Mar 2006 21:07:17 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >Mark McIntyre <> writes:
    >> On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
    >> <> wrote:
    >>>Then why gets() is there?

    >>
    >> Because when the language was first designed, it was found useful by
    >> the *highly expert* people using it, in the *tightly controlled*
    >> environment they used.

    >
    >I'm not convinced that the old environments were that tightly
    >controlled.


    I was thinking of the *original* implementation, ie a half dozen guys
    working on a single machine with total control over what they did.

    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Mar 29, 2006
    #10
  11. jaysome Guest

    Mark McIntyre wrote:
    > On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
    > <> wrote:
    >
    >
    >>Then why gets() is there?

    >
    >
    > Because when the language was first designed, it was found useful by
    > the *highly expert* people using it, in the *tightly controlled*
    > environment they used.
    >
    > Nowadays its there to avoid breaking legacy code, even though its
    > impossible to use safely by anyone other than an expert operating in a
    > completely controlled environment.
    >
    > Mark McIntyre


    When I write quick-and-dirty programs that no one other than myself will
    use, gets() can be handy. In my favorite hosted environment, sometimes
    this happens: The console window, in which my program outputs its text,
    opens, but closes immediately, before I have a chance to read my program
    output.

    In this case, adding something like the following:

    {
    char a[9];
    gets(a);
    }

    cuts to the chase (i.e., console stays around waiting for your input)
    faster than any other technique.

    After I've perused the output and possibly copied it to a text file, I
    can just hit Enter and be done. Or I can hold down the 'u' key for
    minutes and then press Enter and see what undefined behavior is like. I
    always do the latter, because I know that my implementations and OSes
    that I use would never do anything like format my hard drive or smoke my
    video card. And this is acceptable behavior to me.

    On the other hand, one should never use gets() in, for example, a
    program that is to be distributed to others. The reason is simple--the
    inherent undefined behavior will inevitably lead to unacceptable
    behavior, arguably promptly voiced by the users of said program.

    (I once came across some sample code from Apple that used gets() with a
    really huge buffer (something like 32 KB). That's one of the extremes
    that people will go to in order to try to use gets(). I recall running
    the program and holding a key down for minutes just to see what would
    happen ... funny how I used to be a Mac addict :^)

    Mr. MacIntyre is entirely correct when he says that you have to be an
    expert to be able to legitimately use gets(). When esteemed regulars say
    that gets() is dangerous, they mean it. And when they say to never use
    gets(), they mean it. Kinda Sorta.

    --
    jay
     
    jaysome, Mar 30, 2006
    #11
  12. Richard Bos Guest

    jaysome <> wrote:

    > Mark McIntyre wrote:
    > > On 29 Mar 2006 02:01:23 -0800, in comp.lang.c , "sonu"
    > > <> wrote:
    > >
    > >>Then why gets() is there?

    > >
    > > Because when the language was first designed, it was found useful by
    > > the *highly expert* people using it, in the *tightly controlled*
    > > environment they used.
    > >
    > > Nowadays its there to avoid breaking legacy code, even though its
    > > impossible to use safely by anyone other than an expert operating in a
    > > completely controlled environment.

    >
    > When I write quick-and-dirty programs that no one other than myself will
    > use, gets() can be handy.


    The problem is ensuring that noone else _will_ use it, even
    accidentally. Nor that you'll use it three weeks on, when you've
    forgotten the limit. And then there's the problem of the cat on the
    keyboard...

    > In my favorite hosted environment, sometimes
    > this happens: The console window, in which my program outputs its text,
    > opens, but closes immediately, before I have a chance to read my program
    > output.
    >
    > In this case, adding something like the following:
    >
    > {
    > char a[9];
    > gets(a);
    > }
    >
    > cuts to the chase (i.e., console stays around waiting for your input)
    > faster than any other technique.


    Does it? I find it unacceptably verbose. Here, have a scream and leap:

    getchar();

    > After I've perused the output and possibly copied it to a text file, I
    > can just hit Enter and be done. Or I can hold down the 'u' key for
    > minutes and then press Enter and see what undefined behavior is like. I
    > always do the latter, because I know that my implementations and OSes
    > that I use would never do anything like format my hard drive or smoke my
    > video card.


    You must be using VMS, then. Or possibly an AS/400. Under anything from
    either Micro$oft or the Open Sores community, I wouldn't be so sure.

    > Mr. MacIntyre is entirely correct when he says that you have to be an
    > expert to be able to legitimately use gets(). When esteemed regulars say
    > that gets() is dangerous, they mean it. And when they say to never use
    > gets(), they mean it. Kinda Sorta.


    Nothing Kinda Sorta about it. Never use gets(). Full stop.

    Richard
     
    Richard Bos, Mar 30, 2006
    #12
  13. jaysome Guest

    Richard Bos wrote:

    >>Mark McIntyre wrote:

    [snip]
    >>In my favorite hosted environment, sometimes
    >>this happens: The console window, in which my program outputs its text,
    >>opens, but closes immediately, before I have a chance to read my program
    >>output.
    >>
    >>In this case, adding something like the following:
    >>
    >>{
    >>char a[9];
    >>gets(a);
    >>}
    >>
    >>cuts to the chase (i.e., console stays around waiting for your input)
    >>faster than any other technique.

    >
    >
    > Does it? I find it unacceptably verbose. Here, have a scream and leap:
    >
    > getchar();


    That's arguably better. But I inevitably forget what it's prototype is
    compared to getc(). Overall, it's faster for me to use gets() than to
    wait for Help on getc() or getchar() to arrive. And overall, it does not
    matter to me.

    All I can say is YMMV. And others' as well.

    --
    jay
     
    jaysome, Mar 30, 2006
    #13
  14. jaysome Guest

    Richard Bos wrote:
    [snip]
    > You must be using VMS, then. Or possibly an AS/400. Under anything from
    > either Micro$oft or the Open Sores community, I wouldn't be so sure.


    I used to use VMS, but now I use Windows XP.

    I've also used Microsoft Windows 95/98/ME/NT/2K, and use of gets() would
    not cause any problems in any of these OSes, let alone XP. For anyone
    who disagrees, please provide the year, make and model of your
    antiquated compiler.

    --
    jay
    Everyone loves the '$' sign. Everyone loves Raymond, too.
     
    jaysome, Mar 30, 2006
    #14
    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. The Devil's Advocate©

    Reversing a marquee tag

    The Devil's Advocate©, Oct 11, 2004, in forum: HTML
    Replies:
    22
    Views:
    2,390
    Sentient Fluid
    Oct 23, 2004
  2. Kevin

    Help: 2D Array Reversing

    Kevin, Oct 3, 2003, in forum: C++
    Replies:
    4
    Views:
    1,039
    Thomas Matthews
    Oct 6, 2003
  3. Dr. David Kirkby

    Reversing the order of some loops.

    Dr. David Kirkby, Oct 22, 2003, in forum: C Programming
    Replies:
    4
    Views:
    333
    Glen Herrmannsfeldt
    Oct 26, 2003
  4. netforce

    reversing a string - newbie question

    netforce, Mar 2, 2004, in forum: C Programming
    Replies:
    9
    Views:
    541
    Old Wolf
    Mar 8, 2004
  5. flipflop

    Reversing order of quicksort

    flipflop, May 28, 2004, in forum: C Programming
    Replies:
    3
    Views:
    364
    Martin Ambuhl
    May 28, 2004
Loading...

Share This Page