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

user884685
  • 47
  • 2
  • 4
  • 8

2 Answers2

2

You have two critical errors in your code. First of all, bufferFromStdin does not point to allocated memory. scanf requires a pointer to memory that has been previously allocated (such as buf in your example) where it will store the result. When you pass an uninitialized variable such as bufferFromStdin the result is undefined.

Second, the == operator in C compares two pointers, not what they are pointing to. Therefore, buf == "THIS" will never be true, since buf isn't pointing to a constant array. In order to compare two strings, use strcmp. Of course, both pointers need to point to something for this to work.

Also, as a side note, main should always return int not void.

Once you correct those two problems, your code should work:

int main(int argc, char * argv[])
{ 
  char buf[100];
  printf("Enter something:\n");
  scanf("%s", buf);
  printf("First scan from stdin is: %s\n", buf);

  if(strcmp(buf, "THIS") == 0) {
    printf("THIS found first\n");
  } else {printf("Not Found first\n");}

  return 0;
}
epsalon
  • 2,294
  • 12
  • 19
0

you have a couple of programming errors in your code.

void main(int argc, char * argv[])
{ 
  FILE* inFile = NULL;
  char * bufferFromStdin;
  char buf[100];

you are allocating three pointer variables, using three different styles:

  • inFile is properly initialized (NULL pointer).
  • bufferFromStdin is not initialized.
  • buf is initialized and points to a statically allocated area.

you probably want to check how one allocates memory in C.

  printf("Enter something:\n");
  scanf("%s", buf);

this scanf call is just a bit dangerous. if the user inserts a string that is longer than 99 chars (remember the \0 terminator), it will overflow the buffer. you probably want to check this question.

  printf("First scan from stdin is: %s\n", buf);

  if(buf == "THIS" || buf[0]=='T')

I'm sorry to say, but I find this quite funny! you want to test string equality (content, not pointer) and you see the first test does not work (you are comparing pointers) so you add a second test. check the manual pages.

   {
    printf("THIS found first\n");
   }
   else {printf("Not Found first\n");}

I find this also a bit ugly. you might want to stick to one style for indenting.

   printf("Enter something again:\n");
   scanf("%s", bufferFromStdin);

you are using your uninitialized pointer. what happens here depends on your programming environment, operating system, compiler. a strong operating system will probably terminate your program. or you are corrupting some memory areas.

   printf("Second scan from stdin is: %s\n", bufferFromStdin);
   if(bufferFromStdin == "THIS")

same problem as above, but you forgot to implement your workaround.

   {
    printf("THIS found second\n");
   }
   else {printf("Not Found second\n");}
}//main
Community
  • 1
  • 1
mariotomo
  • 9,438
  • 8
  • 47
  • 66