getchar vs. cin

Discussion in 'C++' started by Kristo, Mar 23, 2005.

  1. Kristo

    Kristo Guest

    Is there a preference between getchar and cin for reading one character
    at a time from stdin? TC++PL chapter 6 has a parser that uses getchar,
    but I suspect that the purists will say to always go with cin.
    Naturally, the former is probably faster. However, in the context of
    writing a parser, I think istream's skipws and noskipws manipulators
    could be useful. Opinions?

    Kristo
     
    Kristo, Mar 23, 2005
    #1
    1. Advertising

  2. Kristo wrote:
    > Is there a preference between getchar and cin for reading one character
    > at a time from stdin? TC++PL chapter 6 has a parser that uses getchar,
    > but I suspect that the purists will say to always go with cin.
    > Naturally, the former is probably faster. However, in the context of
    > writing a parser, I think istream's skipws and noskipws manipulators
    > could be useful. Opinions?
    >
    > Kristo
    >


    All depends on the features you want in your
    stream handler.

    The C++ streams have better exception handling
    than the C versions; however, the C versions have
    less overhead than the C++ streams.

    The C++ streams are more adaptable to different
    situations than the C streams.

    You would need more information about the I/O
    to make a good decision. On some platforms, you
    may be using O.S. functionality rather than
    the C++ streams.

    Text applications have different requirements than
    windowing, I/O to devices, or file I/O.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Mar 24, 2005
    #2
    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. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,150
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,267
    Kevin Saff
    Jan 6, 2004
  3. Cam

    Use of getchar

    Cam, May 30, 2004, in forum: C++
    Replies:
    3
    Views:
    539
    John Carson
    May 30, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,798
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,849
    Fernando
    Nov 16, 2011
Loading...

Share This Page