title: Writing and Reading to and from a binary file C language
body: <p>I was given an assignment to manage a sort of music store. In the database (which is saved as a .dat file) we have an artists name, and the album.</p>
  
  <p>I'm having problems writing and reading the file.
  First thing is, even if I don't write anything, just create the file, and then open the file in notepad, i see gibberish and letters in chinese or japanese.
  Even if i write to the fail, or read from it using visual studio, this doesnt seem to change. Here's my code:</p>
  
  <p>I opened the file with:</p>
  
  <pre><code>p=fopen("database.dat","w+");
  </code></pre>
  
  <p>The add item function:</p>
  
  <pre><code>void add_item(char* artist,char* record,FILE* p) //adds an item with artist and record to store
  {
      item node;
      int item_size=sizeof(item);
      rewind(p);
      strcpy(node.artist,artist);
      strcpy(node.record,record);
      fwrite(&amp;node,item_size,1,p);
      printf("Data added\n");
  }
  </code></pre>
  
  <p>item is the struct that is used to define a single item in the store. it has 2 fields, string artist and string record.</p>
  
  <pre><code>typedef struct item
  {
      char artist[100],record[100];
  }item;
  </code></pre>
  
  <p>This is for reading:</p>
  
  <pre><code>void print_file(FILE* p) //print the entire file
  {
      int size=sizeof(item);
      item node;
      rewind(p);
      while(!feof(p))
      {
          fread(&amp;node,size,1,p);
          printf("%s - %s\n",node.artist,node.record);
      }
  }
  </code></pre>
  
  <p>if i use print_file, i see gibberish, if i actually open the file with notepad, i see japanese.</p>
  
  <p>Help! :D</p>
  
  <p>edit: Just discovered something. If I add an item, and then read the file, i will read the item. But if i run the program again, and try to read the file immediatly, i see gibberish.</p>