title: Read and write struct with unistd.h read/write
body: <p>I'm studying UNIX programming and was experimenting with read/write system calls.
  I have a file with a pair of integer:</p>
  
  <pre><code>4 5
  </code></pre>
  
  <p>and I wrote this code to read the numbers:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  
  typedef struct prova {
      int first;
      int second;
  } prova_t;
  
  int main(void) {
      int fd;
  prova_t origin;
  prova_t result;
  ssize_t bytes_read;
  size_t nbytes;
  
  fd = open("file.bin", O_WRONLY | O_CREAT);
  origin.first = 24;
  origin.second = 3;
  write(fd, &amp;origin, sizeof(prova_t));
  close(fd);
  
  
  fd = open("file.bin", O_RDONLY);
  nbytes = sizeof(prova_t);
  /* 1.BAD */
  bytes_read = read(fd, &amp;result, nbytes);
  write(STDOUT_FILENO, &amp;(result.first), sizeof(int));
  write(STDOUT_FILENO, &amp;(result.second), sizeof(int));
  close(fd);
  
      /* 2.GOOD */
      nbytes = sizeof(int);
      bytes_read = read(fd, &amp;(result.first), nbytes);
      write(STDOUT_FILENO, &amp;(result.first), bytes_read);
      bytes_read = read(fd, &amp;(result.second), nbytes);
      write(STDOUT_FILENO, &amp;(result.second), bytes_read);
  
      return 0;
  }
  </code></pre>
  
  <p>In my first attempt I tried to read the whole struct from file and write its members to stdout. In this way, along with the numbers, I get some weird characters</p>
  
  <pre><code>4 5
  E�^�
  </code></pre>
  
  <p>In my second attempt I read the numbers one by one and there were no problems in the output.</p>
  
  <p>Is there any way to read and write the struct using the first method?</p>
  
  <p><em>Edit: I updated the code to reflect suggestion from other users but still getting strange characters instead of numbers</em></p>