title: How do you convert binary file to bytes?
body: <p>I'm building a simple ftp which allows the user to ask basic commands of a unix server (eg: <code>cd home/file/</code>) and upload files to the server and download files from the server. I have gotten the basic commands to work and print between client and server, but copying files that aren't text is a challenge. </p>
  
  <p>My current code for the <code>get</code> function is as follows:</p>
  
  <p><strong>Cli6.c</strong></p>
  
  <pre><code>    else if (strcmp(tokens[0], "get") == 0) {
          char *filecpy = convertfile("mtg.jpg");
          printf("%s .... %d\n", filecpy, strlen(filecpy));
      }
  </code></pre>
  
  <p><strong>FTP.c</strong></p>
  
  <pre><code>char *convertfile(const char *filename) {
      FILE *fileptr; 
      char *buffer;
      long filen;
      size_t result;
       
      fileptr = fopen(filename, "rb");
       
      fseek(fileptr, 0, SEEK_END);
      filen = ftell(fileptr);
      rewind(fileptr);
       
      printf("File size: %d\n", filen);
       
      buffer = (char *)malloc(filen + 1); // this is only allocating 8bytes
       
      fread(buffer,filen, 1,fileptr);
      printf("Buffer: %s\n", buffer);
      close(fileptr);
       
       
      /*
      char *c; 
      char *buffer;
      fileptr = fopen(filename, "rb");
      while (c != EOF) {
          fread(&amp;c, (size_t)1, (size_t)1, fileptr);
          buffer[c]++;
          c = fgetc(fileptr);
          sprintf(buffer[strlen(buffer)], "%c", c);
      }
      close(fileptr);
      */         
       
      return buffer;
  }
   
  //convert bytes to file
  void convertbytes(const char *buffer, const char *filename) {
      FILE *fileptr; 
      printf("%s .... %d\n", buffer, strlen(buffer));
      fileptr = fopen(filename, "wb");
      //for (int i = 0; i &lt; sizeof(buffer); i++) {
      fwrite(buffer, 1, strlen(buffer), fileptr);
          //printf("%c :: %d\n", buffer[i], i);
      //}
       
      close(fileptr);
  }
  </code></pre>
  
  <p>Right now, I'm only trying to copy the files correctly, so no sending over to server yet. With this code, I can copy text files fine, but another file, such as a .o file containing 6.0kb, will be copied with 8 bytes. I'm attempting to convert the file to bytes (ready to send somewhere) and then building a file from bytes. Am I approaching this wrong? Should I be using other means to do this? What is the correct way of build a simple FTP? </p>