title: File operations. A c program that will take a single command line argument using fork() execvp() , wait/waitpid()
body: <p>I was writing a program that could pass in a command-line argument in c program. The linux command are stored in a file called "sample-input". It only contains cp file1 dest1 or mv file2 dest2. My problem is when I tokenize the string in the file. It seems like it only reading the first line of the file. Here is my code. Can someone help me, please?</p>
  
  <pre><code>error message i got (after edit):
  
      temp1/f4.txt
   temp1/f5.txt
   temp1/f6.txt
  -temp1/f7.c
  +temp1/f8.c
  +temp2/f1.txt
   temp2/f2.c
  +temp2/f3.txt
   temp2/f4.txt
  +temp2/f5.txt
   temp2/f6.txt
   temp2/f9.c
  Test Result: [Failed]
  
  
      my sample-input
      cp temp1/f1.txt              temp2/
      mv temp1/f2.c                           temp2/
      cp temp1/f3.txt                temp2/
      cp temp1/f4.txt                temp2/
      cp temp1/f5.txt                temp2/
      cp temp1/f6.txt                 temp2/
      mv temp1/f7.c                   temp1/f8.c
      mv temp1/f9.c                   temp2/
      cp temp1/f1.txt              temp1/f1a.txt
  </code></pre>
  
  <p>My current code.</p>
  
  <pre><code>   /* Your code goes here */
  
  #define _GNU_SOURCE
  #include &lt;string.h&gt;
  # include &lt;stdio.h&gt;
  # include &lt;stdlib.h&gt;
  # include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  
  int main(int argc, char* argv[])
  {
      FILE* fp;
      fp = fopen("sample-input", "r");
  
      if (fp == NULL) {
          fprintf(stderr, "An error has occured1\n");
          exit(1);
      }
      if (argc != 2) {
          fprintf(stderr, "argc=%d\n", argc);
          fprintf(stderr, "An error has occured2 \n");
          exit(1);
      }
      else if (argc == 2) {
          char ag[1024];
  
          while (fgets(ag, 1024, fp) != NULL)
          {
              fgets(ag, 1024, fp);
  
              char* array[4];
              int i = 0;
              char* token = strtok(ag, " \t\n");
  
              while (token != NULL) {
  
                  array[i] = token;
                  i++;
                  token = strtok(NULL, " \t\n");
              }
              array[i] = NULL;
              int r = fork();
  
              if (r == 0) {
                  execvp(array[0], array);
              }
              else if (r &lt; 0) {
  
                  fprintf(stderr, "An error an occoured\n");
                  exit(1);
  
              }
              else {
  
                  wait(NULL);
  
              }
  
  
          }
  
  
  
  
      }
      return 0;
  }
  </code></pre>