Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Reading from stdin

Reading from stdin

August 19, 2021 by James Palmer

You can do something like this to read 10 bytes:
char buffer[10];
read(STDIN_FILENO, buffer, 10);

remember read() doesn’t add ‘’ to terminate to make it string (just gives raw buffer).
To read 1 byte at a time:
char ch;
while(read(STDIN_FILENO, &ch, 1) > 0)
{
//do stuff
}

and don’t forget to #include , STDIN_FILENO defined as macro in this file.
There are three standard POSIX file descriptors, corresponding to the three standard streams, which presumably every process should expect to have:
Integer value Name
0 Standard input (stdin)
1 Standard output (stdout)
2 Standard error (stderr)

So instead STDIN_FILENO you can use 0.
Edit:
In Linux System you can find this using following command:
$ sudo grep ‘STDIN_FILENO’ /usr/include/* -R | grep ‘define’
/usr/include/unistd.h:#define STDIN_FILENO 0 /* Standard input. */

Notice the comment /* Standard input. */

From the man read:
#include
ssize_t read(int fd, void *buf, size_t count);

Input parameters:

int fd file descriptor is an integer and not a file pointer. The file descriptor for stdin is 0
void *buf pointer to buffer to store characters read by the read function
size_t count maximum number of characters to read

So you can read character by character with the following code:
char buf[1];

while(read(0, buf, sizeof(buf))>0) {
// read() here read from stdin charachter by character
// the buf[0] contains the character got by read()
….
}

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in