size

Proper/Efficient way to determine size of stdin in C

落花浮王杯 提交于 2020-11-29 03:53:12
问题 Based on my previous question, what would be the proper yet efficient way to determine the size of stdin if stdin is coming from a pipe or a terminal on different systems. I was doing the following, however, based on some comments, it is not the right way, and it might or it might not work on different systems. #include <sys/stat.h> off_t size(FILE *st_in) { struct stat st; if (fstat(fileno(st_in), &st) == 0) return st.st_size; return -1; } 回答1: You can't. Imagine stdin is like a water tap.