title: retrofitting a .h & .c file to my already working .c program
body: <p>I have a program that I'm doing for class where I need to take the content of one file, reverse it, and write that reversed content to another file. I have written a program that successfully does this (after much googling as I am new to the C programming language). The problem however is that my professor wants us to submit the program in a certain way with a couple supporting .h and .c files (which I understand is good practice). So I was hoping someone could help me understand exactly how I can take my already existing program and make it into one that is to his specifications, which are as follows:</p>
  
  <ul>
  <li><p>he would like a file named "file_utils.h" that has function signatures and guards for the following two functions</p>
  
  <p>int read_file( char* filename, char **buffer );</p>
  
  <p>int write_file( char* filename, char *buffer, int size);</p></li>
  </ul>
  
  <p>thus far I have created this file to try and accomplish this.</p>
  
  <pre><code>#ifndef UTILS_H
  #define UTILS_H
  
      int read_file(char* filename, char **buffer);
      int write_file(char* filename, char *buffer, int size);
  
  #endif
  </code></pre>
  
  <ul>
  <li>he would like a file named "file_utils.c" that has the implemented code for the previous two functions </li>
  <li>he would like a file named "reverse.c" that accepts command arguments, includes a main function, and calls the functions from the previous two files.</li>
  </ul>
  
  <p>now. I understand how this is supposed to work, but as I'm looking at the program I wrote my way I'm unsure how to actually accomplish the same result by adhering to the previously mentioned specifications.</p>
  
  <p>Below is the program that successfully accomplishes the desired functionality </p>
  
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;string.h&gt;
  #include&lt;sys/stat.h&gt;
  #include&lt;unistd.h&gt;
  
  int main(int argc, char *argv[]) {
  
      int file1, file2, char_count, x, k;
      char buffer;
  
      // if the number of parameters passed are not correct, exit
      //
      if (argc != 3) {
              fprintf(stderr, "usage %s &lt;file1&gt; &lt;file2&gt;", argv[0]);
              exit(EXIT_FAILURE);
      }
      // if the origin file cannot be opened for whatever reason, exit
      // S_IRUSR specifies that this file is to be read by only the file owner
      //
      if ((file1 = open(argv[1], S_IRUSR)) &lt; 0) {
              fprintf(stderr, "The origin-file is inaccessible");
              exit(EXIT_FAILURE);
      }
      // if the destination-file cannot be opened for whatever reason, exit
      // S_IWUSR specifies that this file is to be written to by only the file owner
      //
      if ((file2 = creat(argv[2], S_IWUSR)) &lt; 0) {
              fprintf(stderr, "The destination-file is inaccessible");
              exit(EXIT_FAILURE);
      }
      // SEEK_END is used to place the read/write pointer at the end of the file
      //
      char_count = lseek(file1, (off_t) 0, SEEK_END);
      printf("origin-file size is %d\n", char_count - 1);
  
      for (k = char_count - 1; k &gt;= 0; k--) {
              lseek(file1, (off_t) k, SEEK_SET);
  
              x = read(file1, &amp;buffer, 1);
  
              if (x != 1) {
                      fprintf(stderr, "can't read 1 byte");
                      exit(-1);
              }
  
              x = write(file2, &amp;buffer, 1);
              if (x != 1) {
                      fprintf(stderr, "can't write 1 byte");
                      exit(-1);
  
              }
      }
      write(STDOUT_FILENO, "Reversal &amp; Transfer Complete\n", 5);
      close(file1);
      close(file2);
  
      return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>any insight as to how I can accomplish this "re-factoring" of sorts would be much appreciated, thanks!</p>