Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikAizuddin/820f2c6466cfd1cd369b to your computer and use it in GitHub Desktop.
Save nikAizuddin/820f2c6466cfd1cd369b to your computer and use it in GitHub Desktop.
Shows how to read a string that have "space" character.
/*=====================================================================
+-------------------------------------------------------------------+
| |
| Example how to read "Hello World" from stdin |
| |
| The main problem with scanf() is it unable to read properly |
| a string that have "space" character. |
| |
| This example will show how to read a string that have "space" |
| character. |
| |
| However, this example uses Low-Level I/O |
| |
| For further information about _read() and _write(), please |
| visit the Microsoft Developer Network: |
| http://msdn.microsoft.com/en-us/library/wyssk1bs.aspx |
| http://msdn.microsoft.com/en-us/library/1570wh78.aspx |
| |
+-------------------------------------------------------------------+
===================================================================*/
#include <io.h> /* Used for _read(), _write() */
#define STDIN 0
#define STDOUT 1
#define BUFFER_SIZE 1024
char readbuffer[BUFFER_SIZE];
int main(void)
{
int bytes_read = 0; /* stores number of bytes read from stdin */
/** Read ascii string from stdin and store into readbuffer */
bytes_read = _read(STDIN, /*Read data from stdin*/
readbuffer, /*Store the data to readbuffer*/
BUFFER_SIZE); /*Number of bytes to read*/
/** Print the ascii string stored in readbuffer */
_write(STDOUT, /*Write data to stdout*/
readbuffer, /*Write the data from readbuffer*/
bytes_read); /*Number of bytes to write*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment