I/O question

Discussion in 'C++' started by junw2000@gmail.com, Jun 8, 2006.

  1. Guest

    How input infinite stream of characters from the keyboard?
    Will the below code work?

    char *buf;

    while(cin>>buf) {

    //process buf

    }

    I need to process each char typed individually untill End Of Input is
    input.
    Thanks.
     
    , Jun 8, 2006
    #1
    1. Advertising

  2. wrote:
    > How input infinite stream of characters from the keyboard?
    > Will the below code work?


    No, definitely not. 'buf' is uninitialised. Any attempt to read "into"
    it has undefined behaviour. Perhaps you should look into 'get' member
    of 'istream'...

    > char *buf;
    >
    > while(cin>>buf) {
    >
    > //process buf
    >
    > }
    >
    > I need to process each char typed individually untill End Of Input is
    > input.


    Usually you need to check the error state, and if the stream is 'good',
    then read the next char. Something like

    while (cin.good()) {
    char nextchar = cin.get();
    if (!cin.eof()) {
    // do something with 'nextchar'
    }
    }

    What book on I/O are you reading that doesn't explain such things?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 8, 2006
    #2
    1. Advertising

  3. Default User Guest

    wrote:

    > How input infinite stream of characters from the keyboard?
    > Will the below code work?
    >
    > char *buf;
    >
    > while(cin>>buf) {
    >
    > //process buf
    >
    > }
    >
    > I need to process each char typed individually untill End Of Input is
    > input.
    > Thanks.




    No. You have a char pointer, buf, that points to no storage. Any
    attempt to read into it is undefined behavior.

    If you truly need to process one character at a time, you can use:


    int c;

    c = cin.get();


    get() returns either the next character or EOF if there's no more
    input, so you can put it in a loop. You can also read larger amounts
    into a buffer, but you either need to provide storage or use a
    container like std::string.




    Brian
     
    Default User, Jun 8, 2006
    #3
  4. Guest

    Default User wrote:
    > wrote:
    >
    > > How input infinite stream of characters from the keyboard?
    > > Will the below code work?
    > >
    > > char *buf;
    > >
    > > while(cin>>buf) {
    > >
    > > //process buf
    > >
    > > }
    > >
    > > I need to process each char typed individually untill End Of Input is
    > > input.
    > > Thanks.

    >
    >
    >
    > No. You have a char pointer, buf, that points to no storage. Any
    > attempt to read into it is undefined behavior.
    >
    > If you truly need to process one character at a time, you can use:
    >
    >
    > int c;
    >
    > c = cin.get();
    >
    >
    > get() returns either the next character or EOF if there's no more
    > input, so you can put it in a loop. You can also read larger amounts
    > into a buffer, but you either need to provide storage or use a
    > container like std::string.
    >


    How about this:

    char c;
    while((c = std::cin.get()) != EOF ){

    //do something with c

    }

    But I find that for the second time while() is executed, c='\n'.
     
    , Jun 9, 2006
    #4
  5. Rolf Magnus Guest

    wrote:

    >> int c;
    >>
    >> c = cin.get();
    >>
    >>
    >> get() returns either the next character or EOF if there's no more
    >> input, so you can put it in a loop. You can also read larger amounts
    >> into a buffer, but you either need to provide storage or use a
    >> container like std::string.
    >>

    >
    > How about this:
    >
    > char c;
    > while((c = std::cin.get()) != EOF ){
    >
    > //do something with c
    >
    > }



    This will only work if EOF happens to be a valid char value. You should use
    std::istream::int_type for c.

    > But I find that for the second time while() is executed, c='\n'.
     
    Rolf Magnus, Jun 9, 2006
    #5
  6. Default User Guest

    wrote:

    >
    > Default User wrote:
    > > wrote:
    > >
    > > > How input infinite stream of characters from the keyboard?


    > > If you truly need to process one character at a time, you can use:
    > >
    > >
    > > int c;
    > >
    > > c = cin.get();
    > >
    > >
    > > get() returns either the next character or EOF if there's no more
    > > input, so you can put it in a loop. You can also read larger amounts
    > > into a buffer, but you either need to provide storage or use a
    > > container like std::string.
    > >

    >
    > How about this:
    >
    > char c;
    > while((c = std::cin.get()) != EOF ){
    >
    > //do something with c
    >
    > }


    You'll notice that I specified c to be int. There's a reason for that,
    get() returns an int. The reason that it returns an int is that EOF is
    a negative value (often -1 but not required). The char type may or may
    not be signed.

    > But I find that for the second time while() is executed, c='\n'.



    I don't know exactly what you mean. What did you enter?



    Brian
     
    Default User, Jun 9, 2006
    #6
  7. Guest

    Default User wrote:
    > wrote:
    >
    > >
    > > Default User wrote:
    > > > wrote:
    > > >
    > > > > How input infinite stream of characters from the keyboard?

    >
    > > > If you truly need to process one character at a time, you can use:
    > > >
    > > >
    > > > int c;
    > > >
    > > > c = cin.get();
    > > >
    > > >
    > > > get() returns either the next character or EOF if there's no more
    > > > input, so you can put it in a loop. You can also read larger amounts
    > > > into a buffer, but you either need to provide storage or use a
    > > > container like std::string.
    > > >

    > >
    > > How about this:
    > >
    > > char c;
    > > while((c = std::cin.get()) != EOF ){
    > >
    > > //do something with c
    > >
    > > }

    >
    > You'll notice that I specified c to be int. There's a reason for that,
    > get() returns an int. The reason that it returns an int is that EOF is
    > a negative value (often -1 but not required). The char type may or may
    > not be signed.
    >


    > > But I find that for the second time while() is executed, c='\n'.

    > I don't know exactly what you mean. What did you enter?


    I enter a stream, for example, "aabbbbaaa" and a carriage return. I
    think '\n' is from the carriage return. When the second time the
    while() loop is run, I enter another stream. But the '\n' is already
    read, so c='\n'. Am I right? How to get rid of the '\n'?

    Anothe thing is that I enter a stream, "aabbbbaaa". Only after I enter
    the carriage return, the while loop start to process the stream char by
    char. Where is the stream stored before entering the while loop? in a
    buffer?

    Is it possible that when I am typing the stream, "aabbbbaaa", the code
    processes the char one by one untill I enter a carriage return?
     
    , Jun 9, 2006
    #7
  8. Default User Guest

    wrote:

    > Default User wrote:
    > > wrote:


    > > > But I find that for the second time while() is executed, c='\n'.

    > > I don't know exactly what you mean. What did you enter?

    >
    > I enter a stream, for example, "aabbbbaaa" and a carriage return.


    The word you're looking for is "string", not "stream". Correct
    terminology will aid the discussion.

    > I
    > think '\n' is from the carriage return. When the second time the
    > while() loop is run, I enter another stream. But the '\n' is already
    > read, so c='\n'. Am I right? How to get rid of the '\n'?


    I'm not sure what you mean. The loop should have consumed a new-line.

    Here's a complete program:

    #include <iostream>

    int main()
    {
    while(1)
    {
    int c;
    while((c = std::cin.get()) != EOF )
    {
    char ch = c;

    std::cout << "Read: ";
    if (ch == '\n')
    {
    std::cout << "NL";
    }
    if (ch == '\r')
    {
    std::cout << "CR";
    }
    else
    {
    std::cout << ch;
    }

    std::cout << "\n";
    }

    }

    return 0;
    }

    If you run that, you should get similar results to mine:

    12345
    Read: 1
    Read: 2
    Read: 3
    Read: 4
    Read: 5
    Read: NL

    abcde
    Read: a
    Read: b
    Read: c
    Read: d
    Read: e
    Read: NL

    > Anothe thing is that I enter a stream, "aabbbbaaa". Only after I enter
    > the carriage return, the while loop start to process the stream char
    > by char. Where is the stream stored before entering the while loop?
    > in a buffer?


    Yes.

    > Is it possible that when I am typing the stream, "aabbbbaaa", the
    > code processes the char one by one untill I enter a carriage return?


    That's not typical.



    Brian
     
    Default User, Jun 9, 2006
    #8
    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. sean
    Replies:
    1
    Views:
    628
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    697
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    975
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    448
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    882
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page