title: Why fopen() and open() does not recognise the char pointer
body: <p>The purpose of this program is to test the speed of both C library functions and System calls. I have been trying to implement some extra uses such as either inputting from command line arguments like this <code>./scat -l read.txt /dev/null</code> or if command line argument is <code>./scat -l</code> then the program will request the input from you for both read and write files. Using the command line arguments it works perfectly fine but when the program requests for input, <code>fopen()</code> or <code>open()</code> does not recognize the char pointer and says <strong>File not opened successfully</strong>.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char **argv)
  {
      char in;
      int file_type = 1;
      int count_r = 0, count_w = 0; //read, write counts
      char buffer[50]=&quot;&quot;;
  
      while((in = getopt(argc, argv, &quot;ls&quot;)) != -1)
      switch(in)
      {
          case 'l':
          file_type = 1; //C Library call
          break;
  
          case 's':
          file_type = 2;//System call
          break;
  
          default:
          file_type = 1; //default is C library call
      }
      char *source; 
      char *target;
  
      source = (char*)malloc(sizeof(char)*100);
      target = (char*)malloc(sizeof(char)*100);
      
      if(argc&lt;4)
      {
          printf(&quot;Enter the file name: &quot;);
          fgets(source, 100, stdin);
      
          printf(&quot;Enter the target file name: &quot;);
          fgets(target, 100, stdin);
  
          printf(&quot;%s&quot;, source);
          printf(&quot;%s&quot;, target);     
      }
      else
      {
          source = argv[2];
          target = argv[3];
      }
      
      
      if(file_type == 1)
      {
          FILE *file_source = fopen(source, &quot;r&quot;);
  
          printf(&quot;%s&quot;, source);
          if(file_source == NULL)
          {
              printf(&quot;File not opened successfully&quot;);
              return 1;
          }
  
          FILE *file_target = fopen(target, &quot;w&quot;);
  
          if(file_target == NULL)
          {
              printf(&quot;Error in writing to that file&quot;);
              fclose(file_source);
              return 1;
          }
  
          char cpy;
          
          while ((cpy=getc(file_source))!= EOF)
          {
              count_w++;
              putc(cpy, file_target);
          }
  
          if(cpy == EOF)
              count_r = count_w + 1;
  
          fclose(file_source);
          fclose(file_target);
  
          printf(&quot;File copied - C library\n&quot;);
      }
  
      else if(file_type == 2)
      {
          int file_source = open(source, O_RDONLY);
  
          if(file_source == -1)
          {
              printf(&quot;Error opening the file&quot;);
              return 1;
          }
  
          int file_target = open(target, O_CREAT | O_RDWR );
  
          if(file_target == -1)
          {
              printf(&quot;Error in writing to this file&quot;);
              close(file_source);
              return 1;
          }
  
          while(1)
          {
              int bytes = read(file_source, buffer, 1);
              count_r++;
              if(bytes&gt;0)
              {
                  count_w++;
                  write(file_target, buffer, bytes);
              }
              else
                  break;
          }
  
          close(file_source);
          close(file_target);
  
  
          printf(&quot;File copied - System calls\n&quot;);
      }
  
      free(source);
      free(target);
  
      printf(&quot;Read = %d times\n Write = %d times\n&quot;, count_r, count_w);
      return 0;
  }
  </code></pre>