title: Read/write binary file in C++
body: <p>How can I create a linux I/O benchmark for read and write operations of binary file using C++?</p>
  
  <p>I have tried to generate a file of 10MB with the code below but it returns me "segmentation fault (core dumped)".</p>
  
  <pre><code>#define FILE_SIZE 10240
  
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main()
  
  {
      int fd, i;
      int stream[FILE_SIZE];
      double span;
  
      clock_t start,end;
  
      start=clock();
  
      fd=open("data.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
  
      srand(time(NULL));
  
      for(i=0; i&lt;FILE_SIZE; i++){
  
          stream[i]=rand()%2;
          write(fd, (char*)stream[i], strlen((char*)stream[i]));
  
      }
  
      for(i=0; i&lt;FILE_SIZE; i++)
          read(fd, (char*)stream[i], strlen((char*)stream[i]));
  
          close(fd);
  
          end=clock();
          time_lapse=((double)(end-start))/CLOCKS_PER_SEC;
  
          return 0;
  }
  </code></pre>
  
  <p>Then it returns me some warnings due to the conversion included in the write and read functions, I'm sure there is a better way to do this.</p>
  
  <p>Could anyone help me?</p>
  
  <p>Thanks in advance,
  Antonio</p>