I am currently trying to write a program that will read from either stdin from a file (using <) or from piped input. I have the portion using the < working because i am able to read the st_size of the file and create a read() call accordingly:


My question now is how can I use some call of read to read single characters into a buffer? Every attempt I make doesnt seem to work correctly...

Ive tried using read() to read 1 character from stdin into an integer, and then setting buffer to those integers, but this doesnt seem to work. I usually get garbled output using this method, like so:

tag@ubuntu:~/Desktop/ComSci/HW CL 6$ printf 'XR' | ./binsort 2 | od -c
0000000 \b \0 2 \n

All this program was supposed to do was print XR, not these 4 random characters.
Basically, my question is how can I read a variable size piped stdin with read() into a buffer? I must use read() in order to do this...

Edited by tag234: n/a

7 Years
Discussion Span
Last Post by banders7

Check this post in the C code snippets: "Determining if stdin data exists for your program BEFORE issuing fgetc() or similar command." It's on page 4 of the snippets.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.