title: Copying one file to another: scanf keeps looping
body: <p>I am creating a program that takes two command-line arguments: the first line argument is the name of the file to be copied and the second is the new file. If the second argument is missing, copy the file to stdout. If both arguments are missing, the program should read from stdin and print to stdout ie. ./a.out input.txt output.txt</p>
  
  <p>I did the following but I'm facing a problem where scanf keeps looping and does not quit: </p>
  
  <pre><code>int main(int argc, char *argv[]) `
  {
      char text[10];
      FILE *input;
      FILE *output;
      char ch;
  
      printf("%s", argv[0]);
      if (argc == 3)
      {
          input = fopen(argv[1], "r");
          output = fopen(argv[2], "w");
  
          while ((ch = fgetc(input)) != EOF)
          {
              fputc(ch, output);
              ch = fgetc(output);
          }
      }
  
      if (argc == 2)
      {
          input = fopen(argv[1], "r");
  
          while ((ch = fgetc(input)) != EOF)
          {
              printf("%c", ch);
          }
  
          printf("\n");
      }
  
      if (argc == 1)
      {
          scanf("%c", text);
          // here it keeps looping
      }
      fclose(input);
  
      return 0;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/VoTyL.png" rel="nofollow noreferrer">Screen shot with cursor at end of line after some input before return is hit</a></p>