I/O question

J

junw2000

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.
 
V

Victor Bazarov

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
 
D

Default User

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
 
J

junw2000

Default said:
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'.
 
R

Rolf Magnus

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.
 
D

Default User

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
 
J

junw2000

Default said:
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.

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?
 
D

Default User

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top