title: How to write a struct to file in C++ and read the file?
body: <p>I am new. I have some questions, any help would be appreciated.
  I have a struct and write it to a file using the write().</p>
  
  <pre><code>struct PointFull {
      double lat;
      double lon;
  };
  
  PointFull item;
  void* buffer = malloc(sizeof (item));
  int fd = open("output", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
  if (fd &lt; 0) {
      printf("Error opening file\n");
      return 1;
  }
  memcpy(buffer, &amp;item, sizeof (item));
  write(fd2, buffer, sizeof (item));
  </code></pre>
  
  <p>Now I have a file named "output" in hard disk and then I want read the file to test data.</p>
  
  <pre><code>int fd2 = open("output", O_RDONLY, S_IWUSR | S_IRUSR);
  if (fd2 &lt; 0) {
      printf("Error opening file\n");
      return 1;
  }
  void* bufferRead;
  bufferRead = malloc(100);
  read(fd2, bufferRead,100);
  </code></pre>
  
  <p>At the moment, I have bufferRead but I dont know how to read buffer to insert data to struct??? Any help would be appreciated.</p>