title: "Inappropriate file type or format"
body: <p>I am trying to add a new file member with ar_hdr format and put it right after the last element in an archive.  My code compiles, but when I want to view the file name with the ar -t command, I got an error message that says: ar: hello.a: Inappropriate file type or format.  Can someone take a look at my code and give me some hints on how to fix it?  Thanks.</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/utsname.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ar.h&gt;
  
  #define BLOCKSIZE 1
  
  int main (int argc, char **argv)
  {
      char *archive = argv[1];
      char *read_file = argv[2];
  
      int in_fd;
      int out_fd;
  
      char title[] = ARMAG;   //constant define in ar.h
      char buf[BLOCKSIZE];
  
      int num_read;
      int num_written;
  
      struct stat stat_file;
      struct ar_hdr my_ar;
  
      //open read_file (i.e., text file)
      if (stat(read_file, &amp;stat_file) == -1){
          perror("Error in Stat");
          exit(-1);
      }
  
      //assign file info to struct dhr (my_ar)    
      sprintf(my_ar.ar_name, "%s", read_file);
      sprintf(my_ar.ar_date, "%ld", stat_file.st_mtimespec.tv_sec);
      sprintf(my_ar.ar_uid, "%i", stat_file.st_uid);
      sprintf(my_ar.ar_gid, "%i", stat_file.st_gid);
      sprintf(my_ar.ar_mode, "%o", stat_file.st_mode) ;
      sprintf(my_ar.ar_size, "%lld", stat_file.st_size) ;
  
  
      //0666 - open archive
      out_fd = open(archive, O_CREAT | O_WRONLY | O_APPEND, 0666);
      if (out_fd == -1) {
          perror("Canot open/create output file");
          exit(-1);
      }
  
      //write my_ar to archive
      num_written = write(out_fd, title, sizeof(title));
      num_written = write(out_fd, &amp;my_ar, sizeof(my_ar));
  
  
      printf("\n");
  
      return 0;
  }
  </code></pre>