I have a problem finding out how big is the dimension of the stdin through a pipe. I know that a lot of you will be furious at this question, but just hear me out.
Half of it already works:
$ echo "BYE" | ./my_prog
In the linux shell outputs 4 which is exactly what I want.
The problem comes out when I try to feed it some bytes, in fact the first time works while after it doesn't work anymore.
$ ./create_bytes.py -n 200 | ./my_prog
$ 200
$ ./create_bytes.py -n 200 | ./my_prog
$ 0
and I can't understand why. I'm sure the stream is always the same length.
The code I'm using is the following
int main (int argc, char *argv[]) {
struct stat fd_s;
if (fstat(STDIN_FILENO, &fd_s) == -1) {
perror("fstat(fdin)");
exit(EXIT_FAILURE);
}
printf("%lld\n", fdin_stat.st_size);
...
}
Thanks in advance
EDIT: This is the actual request: Read a stream of lines (bytes sequence that terminates with \n) from stdin in 16 bytes blocks. Every line can't be bigger than 128 bytes.
Maybe I'm just making it more difficult than it should be? I hope it can help Thanks
If the input is a pipe, it doesn't have a size. It's a stream that in principle can go on forever. The fact that the first time you ran it it gave you a number is not something you can rely on.
If you want to read everything from
stdininto memory, you need to read data in a loop, and have a buffer that yourealloc()when it is full and there is still more data to be read.If you need to read in a text file and are going to process it line by line, you can consider using the POSIX function
getline(), or you might even read a whole file withgetdelim()if you are sure it doesn't contain a given delimiter.