Hello,

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:

read(0,&inptr,stdinfo.st_size);

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
0000004

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...

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.