Thread: What is the differene between Console I/O function and Stream I/O Function

  1. #1
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39

    Question What is the differene between Console I/O function and Stream I/O Function

    What is the differene between Console I/O function like getchar putchar, gets , puts and Stream I/O Function like cin.get , cout.put ,cin.getline , cout.write ?

    Eg..getchar() is used to take a character and cin.get() is also used to do that....and gets() is used to take string but cin.getline() can also perform that.....plese tell me the difference in these I/O Function..

    though console as well as stream I/O function can be used for I/O....which is good to use......and why....plese explain in simple word .....I am a beginner in c++..

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The getchar putchar, gets , puts functions are C-stdio functions being used to retrieve information from the console.

    The cin.... functions are C++ stream functions that also retrieve information from the console.

    .which is good to use
    In a C++ program I recommend you use the C++ streams. They are much less error prone, IMO. Leave the C-stdio functions for C programs.


    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Console I/O function like getchar putchar, gets , puts
    These are 'C' functions, declared in stdio.h (C) or cstdio (C++)

    > Stream I/O Function like cin.get , cout.put ,cin.getline , cout.write ?
    These are C++ functions, declared in iostream

    If you're writing C++, stick to the latter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    SO is there any difference Between these function ..........i mean to say that getchar() do the same task as cin.get()....or gets() to same task as cin.getline() .......so which is to use and why not to use other ??


    i am asking in c++ programming

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    SO is there any difference Between these function
    Yes getchar() is a C function cin.get() is a C++ function.

    .or gets() to same task as cin.getline()
    Yes gets() is a very very dangerous C function that should never ever be used. Never use any function that doesn't limit the number of characters that it will try to retrieve into a C-string. The C++ cin.getline() function limits the number of characters it will try to retrieve.

    Again if you're writing a C++ forget about the C-stdio functions and use the proper C++ methods. And avoid mixing the two different input methods.

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your question has already been answered, even though you chose to ask it again.

    Yes, C I/O functions can be used to achieve the same things as C++ streams. In C++, you are better off using C++ streams.

    I'll give you a slightly more detailed answer - which you could probably have found by reading up on such things.

    Streams have the advantages over C I/O functions that
    1) they are generic (a C++ function that reads from a generic istream or writes to a generic ostream doesn't care if that stream interacts with a file on a hard drive, a string, a tape drive, or any other storage medium)
    2) they are -with only a few exceptions that beginners don't need to worry about - type safe (stream operations can automatically handle input or output of an int, a float, or anything else without getting confused between them - and if there is confusion due to a programmer error, the compiler will probably report it),
    3) they are extendable (stream operators can be implemented for user-defined types) in a manner that plays nicely with C++ templates.

    In comparison, C I/O functions are
    1) specific to type of device (different functions are needed to read from a file versus reading from a string)
    2) not typesafe (e.g. printf() and similar functions will happily write a float as if it is an int, if that is what the supplied format string specifies - and that causes undefined behaviour),
    3) not extendable (it is possible to write new functions to support different types, but they do not play particularly well with other such functions and do not play well with C++ templates, so it takes a lot more effort to implement such extensions).

    Also gets() is a deprecated C function. It should not be used in C or C++, because it can happily overwrite random areas of memory (if the user types too much) and there is nothing a C programmer can do to prevent that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    so....is there any another difference between these function except that getchar() belong to C function and cin.get() belongs to c++ function.....PLESE HELP....REPLY FAST
    Last edited by Username changed; 12-22-2013 at 08:41 AM.

  8. #8
    Deleted Account
    Join Date
    Oct 2013
    Posts
    39
    plese help me on this topic

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Rohit Kumar View Post
    so....is there any another difference between these function except that getchar() belong to C function and cin.get() belongs to c++ function.....PLESE HELP....REPLY FAST
    I'm sorry that we didn't hit on the exact wording used on the multiple-choice test you're taking, but as you can see from the posts above there are basically no similarities and all differences (at a very high level they are the same in that they are "get information and put it in a variable", but at just about any level lower than that they are different (they work on different objects, with different syntax, and with different properties for things like reading formats)). Perhaps thinking about what has been said will let you answer whatever question it is that you have; or perhaps you can refine your question if there's some aspect that you're still confused about.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    One other difference is that C programmers who decide to learn C++ often insist on using C I/O, keep searching for reasons they should be allowed to do so, and keep asking the same questions (what are the differences?) in the hope that folks will give up and allow them to ignore C++ I/O.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning a stream from a function
    By killme in forum C++ Programming
    Replies: 5
    Last Post: 03-21-2013, 11:55 AM
  2. Stream function compile problem
    By Know_Your_Role in forum C++ Programming
    Replies: 3
    Last Post: 06-02-2009, 12:03 PM
  3. Stream as return function
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 05-24-2006, 10:18 PM
  4. Stream function question
    By 3kgt in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2003, 06:30 PM
  5. eof member function on the stream cin
    By stimpyzu in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2002, 05:00 PM