Input And Output
Input/output (I/O) is how a program interacts with the computers file system. The C/C++ standard libraries provide methods for reading and writing to files.
Some parts of I/O are standard, others are platform specific.
cin And cout
std::cout are both of the type . They are declared in
File descriptors for
std::cout can be found in the header file
<unistd.h>. They are defined as
Input in a C++ program can be grabbed via
cin.get(), I have found it necessary to call
cin.ignore() to remove the still-present new-line character. If this is not done,
cin.get() will keep triggering.
fflush() can be used on an output buffer to write an unwritten data in it’s output buffer to the file. The stream remains open after this function call. It returns
0 if the operation was a success.
select() is used to wait on multiple files for input/output at the same time (and returns whenever one of them has data).
select() is a BSD UNIX function, and therefore not portable to other systems. Ubuntu GCC has this function.
By default, reading from
stdin and writing to
stdout are blocking operations, that is, they don’t return until their job is complete. For input, this is until a character or line had received.
If you desire, you can use non-blocking I/O. I do recommend against it though, and instead suggest you use threads to solve the concurrancy problem (assuming that is why you are wanting non-blocking I/O in the first place). This is because threads and blocking I/O are less computationally intensive, you can run into performance issues when using non-blocking I/O.
There are two common ways of performing non-blocking I/O:
- Use the ncurses library. The disadvanatage with this is that it also effects the terminal display, and things like printf() stop working correctly.
- Directly change the low-level file I/O settings. This can be a little confusing at first, but this method does not upset the terminal like ncurses does.
Directly Modifying The Low-Level Settings
This involves using the terminos library. So make sure you
Detecting Keyboard Presses
The following code can be used to detect keyboard presses, without waiting for a carriage return character.
This work is licensed under a Creative Commons Attribution 4.0 International License .