title: How to write data to a binary file in C
body: <p>I've got a problem when i try to write data to a binary file. This is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;time.h&gt;
  
  typedef struct
  {
  char name[255];
  int quantity;
  float price;
  } product;
  
  int main()
  {
  product x;
  FILE *f;
  strcpy(x.name,"test");
  x.quantity=10;
  x.price=20.0;
  f=fopen("test.txt","wb");
  fwrite(&amp;x,sizeof(x),1,f);
  fclose(f);
  return 0;
  }
  </code></pre>
  
  <p>When I run the program,it only writes the x.name string,ignoring the other 2(quantity and price). I've googled it and this seems to be the correct function to write data to a binary file...but it still doesn't work for me. What should I do?
  Thanks in advance! </p>