title: c programming read() and write() content to file
body: <p>The user should input some file names in the command line and the program will read each file name from <code>argv[]</code> array. I have to perform error checking etc.  </p>
  
  <p>I want to read each filename. For example, if argv[2] is 'myfile.txt', the program should read the content of 'myfile.txt' and store value in <code>char buffer[BUFSIZ]</code> and then write the content of <code>buffer</code> into another file. </p>
  
  <p>However before the content is written, the program should also write the name of the file and the size. Such that the file can be easily extracted later. A bit like the <code>tar</code> function.</p>
  
  <p>The file I write the content of <code>buffer</code>, depending on the number of files added by user, should be a string like:</p>
  
  <pre><code>myfile.txt256Thisisfilecontentmyfile2.txt156Thisisfile2content..............
  </code></pre>
  
  <p>My question is </p>
  
  <p><strong>1)</strong> How do I write value of <code>argv[2]</code> into file using <code>write()</code> statement, as having problems writing char array, what should I put as (sizeof(?)) inside <code>write()</code>. see below as I don't know the length of the file name entered by the user.</p>
  
  <p><strong>2)</strong> Do I use the '&amp;' to write an integer value into file after name, for example write 4 bytes after file name for the size of file</p>
  
  <p>Here is the code I have written,</p>
  
  <pre><code>char buffer[BUFSIZ];
  int numfiles=5; //say this is no of files user entered at command
  open(file.....
  
  lseek(fdout, 0, SEEK_SET); //start begging of file and move along each file some for loop
  
  for(i=0-; ......
  //for each file write filename,filesize,data....filename,filesize,data......
  int bytesread=read(argv[i],buffer,sizeof(buffer));
  write(outputfile, argv[i], sizeof(argv)); //write filename size of enough to store value of filename
  write(outputfile, &amp;bytesread, sizeof(bytesread));
  write(outputfile, buffer, sizeof(buffer));
  </code></pre>
  
  <p>But the code is not working as I expected. </p>
  
  <p>Any suggestions?</p>