void main(int argc, char * argv[])
{
FILE* inFile = NULL;
char * bufferFromStdin;
char buf[100];
printf("Enter something:\n");
scanf("%s", buf);
printf("First scan from stdin is: %s\n", buf);
if(buf == "THIS" || buf[0]=='T')
{
printf("THIS found first\n");
}
else {printf("Not Found first\n");}
printf("Enter something again:\n");
scanf("%s", bufferFromStdin);
printf("Second scan from stdin is: %s\n", bufferFromStdin);
if(bufferFromStdin == "THIS")
{
printf("THIS found second\n");
}
else {printf("Not Found second\n");}
}//main
gives me the output:
./test < testinput.txt
Enter something:
First scan from stdin is: THIS
THIS found first
Enter something again:
Second scan from stdin is: (null)
Not Found second
testinput.txt has one line of text "THIS"
this is what I get when I run the program with input as regular stdin
./test
Enter something:
THIS
First scan from stdin is: THIS
THIS found first
Enter something again:
THIS
Second scan from stdin is: (null)
Not Found second
How come the input cannot be saved to a char* when using either input method and how would I work around this? I need to get input from stdin by the keyboard and redirecting I/O as well. I think it's something to do with malloc();
Thanks for the help.