How to read STDIN into string variable until EOF in C?
Posted
by NovumCoder
on Stack Overflow
See other posts from Stack Overflow
or by NovumCoder
Published on 2010-03-23T00:13:43Z
Indexed on
2010/03/23
0:21 UTC
Read the original article
Hit count: 646
Hi,
im getting "Bus Error" trying to read stdin into a char* variable. I just want to read whole stuff coming over stdin and put it first into a variable, then continue working on the variable.
My Code is as follows:
char* content;
char* c;
while( scanf( "%c", c)) {
strcat( content, c);
}
fprintf( stdout, "Size: %d", strlen( content));
But somehow i always get "Bus error" returned by calling "cat test.txt | myapp", where "myapp" is the compiled code above.
My question is how do i read stdin until EOF into a variable? As you see in the code, i just want to print the size of input coming over stdin, in this case it should be equal to the size of the file "test.txt".
I thought just using scanf would be enough, maybe buffered way to read stdin?
© Stack Overflow or respective owner