473,785 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is cin always the keyboard's input?

question in subject.
does the standard say anything about this or is it platform dependent?
any advice much appeciated
/B

Jul 19 '05 #1
11 8382
Bob Smith wrote:
question in subject.
does the standard say anything about this or is it platform dependent?
any advice much appeciated


Indeed, the whole point of 'cin' is to keep this detail hidden from you. It
is not only platform-dependent, but case-dependent.
e.g., when you do any of the following:

cat file | myapp
myapp < file

these are instructing the system to use the file (more specifically, the
input stream) as cin.
See section 15.17 in:
http://www.parashift.com/c++-faq-lite/input-output.html

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 19 '05 #2
On Wed, 15 Oct 2003 16:00:36 +0300, Bob Smith <bo******@jippi i.fi>
wrote:
question in subject.
does the standard say anything about this or is it platform dependent?


Platform dependent. Often input is a file anyway:

cat "file.txt" | myprog

The above pipes a file into the standard input of myprog (on unix at
least). Alternatively, cin might be a serial port connection (on an
embedded device), or a telepathic connection to your brain. It is just
the standard stream-based input to a program.

Tom
Jul 19 '05 #3


stephan beal wrote:
Bob Smith wrote:

question in subject.
does the standard say anything about this or is it platform dependent?
any advice much appeciated


Indeed, the whole point of 'cin' is to keep this detail hidden from you. It
is not only platform-dependent, but case-dependent.
e.g., when you do any of the following:

cat file | myapp
myapp < file

these are instructing the system to use the file (more specifically, the
input stream) as cin.
See section 15.17 in:
http://www.parashift.com/c++-faq-lite/input-output.html

well here is my problem:
m_reading is a bool value, qDebug(...) is a qt function, qxevent is a
class which can red from streams.
<snip>

if ( !m_reading ){
if ( std::cin.rdbuf( )->in_avail() ){
m_reading = true;
qDebug( "reading... ");
QxEvent e;
while( cin >> e ) {
m_event_queue.p ush( e );
counter++;
}
qDebug( "...read!") ;
m_reading = false;
}else{
qDebug("..no data" );
}
}else{
qDebug("already reading, return to caller");
}
qDebug("ready") ;
</snip>

Now, trying to output data from one program, to this program, like
../testdatamaker|./eventlogger

( testdatamaker is a program for making up testdata for eventlogger )

testdatamaker outputs strings every second, and eventlogger polls the
cin stream continuously, but it never receives any data, it always
returns with "no data".

So the whole idea with my application is that it should read the input
stream, that being any output from any program, piped, and make display
data out of it.

I feel silly to ask but what is going wrong ?*s*

any help *very* much appreciated.
thank's
/B

Jul 19 '05 #4
Bob Smith wrote:
well here is my problem:
m_reading is a bool value, qDebug(...) is a qt function, qxevent is a
class which can red from streams. ....
I feel silly to ask but what is going wrong ?*s*

any help *very* much appreciated.


i'm not a master of streams, so i don't have any suggestions except:
- have you tried using one of Qt's stream classes instead of std::istream?
- are the input fields properly terminated? (i can't say what QxEvent
expects there.) i'm assuming you're using ostream << QxEvent to create the
records, in which case the answer would of course be, "yes."

i can't find QxEvent in the online docs at trolltech, so haven't read up on
this class.

http://doc.trolltech.com/3.2/qxevent.html
redirs me to:
http://doc.trolltech.com/3.2/qevent.html

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 19 '05 #5
While it was 15/10/03 2:00 pm throughout the UK, Bob Smith sprinkled
little black dots on a white screen, and they fell thus:
question in subject.
does the standard say anything about this or is it platform dependent?
any advice much appeciated


Standard C++ knows nothing of keyboards.

cin is the standard input, whatever that may be. Typical examples are
console input, a pipe or a file.

And even console input is seldom the keyboard directly. It is generally
a mechanism that reads input lines and then transmits them to the
program, line by line. Even these input lines need not come straight
from the keyboard - e.g. if the console is in a GUI setting, chances are
it supports copy and paste.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #6
On Wed, 15 Oct 2003 16:18:53 +0300, Bob Smith <bo******@jippi i.fi>
wrote:


stephan beal wrote:
Bob Smith wrote:

question in subject.
does the standard say anything about this or is it platform dependent?
any advice much appeciated

Indeed, the whole point of 'cin' is to keep this detail hidden from you. It
is not only platform-dependent, but case-dependent.
e.g., when you do any of the following:

cat file | myapp
myapp < file

these are instructing the system to use the file (more specifically, the
input stream) as cin.
See section 15.17 in:
http://www.parashift.com/c++-faq-lite/input-output.html

well here is my problem:
m_reading is a bool value, qDebug(...) is a qt function, qxevent is a
class which can red from streams.
<snip>

if ( !m_reading ){
if ( std::cin.rdbuf( )->in_avail() ){


in_avail will generally be 0 if you haven't attempted a read operation
- it usually just returns the number of characters buffered by the
underlying streambuf. You need to
a) Set stdin to non-blocking mode (in an OS specific way)
b) Try reading something from cin to fill the buffer.
m_reading = true;
qDebug( "reading... ");
QxEvent e;
while( cin >> e ) {
m_event_queue.p ush( e );
counter++;
}
Here the stream will be in an error state (eof). All operations on
such a stream will fail. Add here:

if (cin.eof())
{
cin.clear();
}
else
{
//something serious has gone wrong.
qDebug(strerror (errno)); //or whatever.
throw something;
}
qDebug( "...read!") ;
m_reading = false;
}else{
qDebug("..no data" );
}
}else{
qDebug("already reading, return to caller");
}
qDebug("ready") ;
</snip>


Here's my take on the code:

//set stream to non-blocking

if ( !m_reading ){
m_reading = true;
qDebug( "reading... ");
QxEvent e;
while( cin >> e ) {
m_event_queue.p ush( e );
counter++;
}
if (cin.eof())
{
cin.clear(); //clear error state
}
else
{
throw serious_error() ;
}
qDebug( "...read!") ;
m_reading = false;
}else{
qDebug("already reading, return to caller");
}
qDebug("ready") ;

Alternatively, just set the stream to blocking so that process b waits
for data. You could always put the busy wait in its own thread in b.

Usually you use the posix "select" call to handle this kind of code.

Tom
Jul 19 '05 #7
tom_usenet wrote:
in_avail will generally be 0 if you haven't attempted a read operation


Stroustrup says something different in TC++PL. According to him, you can
use in_avail _before_ a read attempt to find out if that read operation
would block.

Jul 19 '05 #8

"Rolf Magnus" <ra******@t-online.de> wrote in message news:bm******** *****@news.t-online.com...
tom_usenet wrote:
in_avail will generally be 0 if you haven't attempted a read operation


Stroustrup says something different in TC++PL. According to him, you can
use in_avail _before_ a read attempt to find out if that read operation
would block.

He does not say that and Rolf is right. What Stroustrup says is that if in_avail
is positive, then you won't block. The inverse is not true, that if in_avail is zero
you will block.

in_avail only tells you if there is characters in the stream's buffer. While you can
be guaranteed of reading that many characters without blocking (because they're
already in the buffer). However if it is zero or negative, it means a low-level read will have
to occur to get something in the buffer. Whether this will block or not is unknown
to the stream buffer classes.

As Rolf said, if you open a stream and you haven't done any reads, there's nothing
in the buffer and in_avail will be zero. The same is probably true if you read exactly
in_avail characters later.
Jul 19 '05 #9


Ron Natalie wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message news:bm******** *****@news.t-online.com...
tom_usenet wrote:

in_avail will generally be 0 if you haven't attempted a read operation

Stroustrup says something different in TC++PL. According to him, you can
use in_avail _before_ a read attempt to find out if that read operation
would block.

He does not say that and Rolf is right. What Stroustrup says is that if in_avail
is positive, then you won't block. The inverse is not true, that if in_avail is zero
you will block.

in_avail only tells you if there is characters in the stream's buffer. While you can
be guaranteed of reading that many characters without blocking (because they're
already in the buffer). However if it is zero or negative, it means a low-level read will have
to occur to get something in the buffer. Whether this will block or not is unknown
to the stream buffer classes.

As Rolf said, if you open a stream and you haven't done any reads, there's nothing
in the buffer and in_avail will be zero. The same is probably true if you read exactly
in_avail characters later.


Hi again,
here is some code
<snip>

//testdatamaker
#include <iostream>
#include <strstream>
#include <unistd.h>
int main(void){
for ( int c = 0; c < 100;c++){
sleep(1);
strstream s;
s << "key " << c << ":data->" << c << endl;
cout << s.str();
}
}



//testdatasink
#include <iostream>
int main(void){
char c;
while ( cin >> c ) cout << c;
}
../testdatamaker|./testdatasink
doesn't work because the testdatamaker sleeps one second for 100 times
and doesn't flush teh cout.
//new testdatamaker
....
cout << s.str();
cout.flush();//<<--note
....
../testdatamaker|./testdatasink

now works.the testdatasink receives the data in real time.
let's test in_avail()
///test in_avail()
#include <iostream>
#include <unistd.h>
bool has_data();
int main(void){
while( 1 ){
if ( has_data() ){
char c;
while( cin >> c ) cout << c;
}else{
cout << "no data..." << endl;
sleep(1);
}

}
}
bool has_data(){
cout << "cin.rdbuf( )->in_avail() returns:" << cin.rdbuf()->in_avail()
<< endl;
return cin.rdbuf()->in_avail();
}
always returns "no data". has_data() always gives zero( 0 ) calling
cin.rdbuf()->in_avail().
in_avail() is broken on my linux box. or perhaps poorly implemented.
need to look for a new way of getting piped data.

any help much appreciated.

/B

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2009
by: Ray | last post by:
I have English Windows XP Pro and Office 2003 Pro on my computer. When I enter data into fields of tables, queries and forms of Access 2003, it automatically switches to Chinese keyboard input. Even though I remove the Chinese keyboard input from Regional applet, the system will automatically reinstall Chinese keyboard input when I enter data in Office 2003. After I switch from Chinese keyboard input back to English keyboard input, it...
23
7753
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
7
10697
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it were a keyboard. I need to be able to identify input from the scanner and keyboard independently. I've looked at DirectX.DirectInput, and using user32.dll to hook into the keyboard messages, but neither method seems to allow for identification of...
4
4530
by: teddysnips | last post by:
I posted yesterday about a project I'm involved in to build a login application using a barcode scanner. I've solved most of the problems, but one remains. The client want to disable keyboard input (except at some remote sites where there won't be a scanner). The session "knows" whether that site should be keyboard enabled, but unfortunately the output from the scanner is in the form of keypresses! So any attempt to capture and...
7
26541
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int nCode, int wParam, int lParam) {
1
6825
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the message queue, regardless of what application has focus. I can identify the keystrokes and input device by registering for raw input (RegisterRawInputDevices) and processing the WM_INPUT message. This gives me the keystrokes and the ability to...
1
11236
by: Louis Cypher | last post by:
I'm working on an application (OEM) using c# that uses input from a keyboard and a USB Barcode Scanner. I need to be able to identify keystrokes from the barcode scanner and remove them from the message queue, regardless of what application has focus. I can identify the keystrokes and input device by registering for raw input (RegisterRawInputDevices) and processing the WM_INPUT message. This gives me the keystrokes and the ability to...
3
3007
by: NaN | last post by:
I've been trying to use _kbhit() but it didn't do what I thought it would from books, "Detects whether a keypress is available for reading." Herbert Schildt says, "If the user has pressed a key, this function returns true(non-0), but does not read the character. If no keystroke is pending, kbhit() returns false (0)." Here is the test code,
8
5302
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control has the focus and handle ctrl-c, ctrl-v, or any other shortcuts. I have the same shortcuts working in my app, but have not determined how to find out which control has focus. Would I set up a loop or code for each control at form level. Any help...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.