title: C: writing the following code into functions
body: <p>Dear respected programmers. Please could you help me (again) on how to put the following code into functions for my program.
  I have read on-line and understand how functions work but when I do it myself it all goes pear shaped/wrong(I am such a noob).
  Please could you help with how to for example to  write the code below into functions.(like opening the input file).</p>
  
  <hr>
  
  <p>My initial code looks like:</p>
  
  <pre><code>main (int argc, char **argv)
  {
   int bytes_read, bytes_written;
   struct stat inode;
   int input_fd, output_fd;
   char buffer[64];
   int eof = 0;
   int i;
  
   /* Check the command line arguments */
   if (argc != 3) 
   {
     printf("syntax is:         %s \n", &lt;fromfile&gt; &lt;tofile&gt;\n", argv[0]);
    exit (1);
   }
  
   /* Check the input file exists and is a file */
   if ((stat(argv[1], &amp;inode) == -1) || (!S_ISREG(inode.st_mode)))
   {
    printf("%s is not a file\n", argv[1]);
    exit(2);
   }
  
   /* Check that the output file doesnt exist */
   if (stat(argv[2], &amp;inode) != -1)
   {
    printf("Warning: The file %s already exists. Not going to overwrite\n", argv[2]);
    exit(2);
   }
  
   /* Open the input file for reading */
   input_fd = open(argv[1], O_RDONLY, 0);
   if (input_fd == -1)
   {
    printf("%s cannot be opened\n", argv[1]);
    exit(3);
   }
  
   output_fd = open(argv[2], O_CREAT | O_WRONLY | O_EXCL , S_IRUSR|S_IWUSR);
   if (output_fd == -1)
   {
    printf("%s cannot be opened\n", argv[2]);
    exit(3);
   }
  
   /* Begin processing the input file here */
   while (!eof)
   {
    bytes_read = read(input_fd, buffer, sizeof(buffer));
  
    if (bytes_read == -1)
    {
     printf("%s cannot be read\n", argv[1]);
     exit(4);
    }
  
    if (bytes_read &gt; &gt; 0)
    {
     bytes_written = write(output_fd, buffer, bytes_read);
     if (bytes_written == -1)
     {
      printf("There was an error writing to the file %s\n",argv[2]);
      exit(4);
     }
  
     if (bytes_written != bytes_read)
     {
      printf("Devistating failure! Bytes have either magically appeared and been written or dissapeard and been skipped.  Data is inconsistant!\n");
      exit(101);
     }
    } 
    else
    {
     eof = 1;
    }
   }
     close(input_fd);
   close(output_fd);
  
  }
  </code></pre>
  
  <hr>
  
  <p>My attempt at opening an output file:</p>
  
  <pre><code>void outputFile(int argc, char **argv)
  {   
      /* Check that the output file doesnt exist */
  
      if (stat(argv[argc-1], &amp;inode) != -1)
      {
          printf("Warning: The file %s already exists. Not going to overwrite\n", argv[argc-1]); 
          return -1;
      }
  
      /*Opening ouput files*/
  
      file_desc_out = open(argv[i],O_CREAT | O_WRONLY | O_EXCL , S_IRUSR|S_IWUSR);
      if(file_desc_out == -1)
      {
          printf("Error: %s cannot be opened. \n",argv[i]);           //insted of argv[2] have pointer i. 
          return -1;
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Any help on how I would now reference to this in my program is appreciated thank you.
  I tried:
  ouputfile (but I cant figure out what goes here and why either).</p>