title: Read and Print out .dat files in C
body: <p>The story is, I have two programs. The first one asks users to input contact data (Name, tel, email) of a number of people and store it in phonebook.dat (This code works)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  enum {SUCCESS, FAIL, MAX_ELEMENT = 40};
  
  // the phone book structure
  typedef struct phoneaddress_t {
  char name[20];
  char tel[11];
  char email[30];
  }phoneaddress;
  
  int main(void)
  {
  FILE *fp;
  phoneaddress phonearr[MAX_ELEMENT];
  int i,n, irc; // return code
  int reval = SUCCESS;
  
  printf("How many contacts do you want to enter (&lt;20)?");
  scanf("%d", &amp;n);
  for (i=0; i&lt;n; i++){
      printf("name:"); scanf("%s",phonearr[i].name);
      printf("tel:"); scanf("%s",phonearr[i].tel);
      printf("email:"); scanf("%s",phonearr[i].email);
  }
  if ((fp = fopen("phonebook.dat","w+b")) == NULL){
      printf("Can not open %s.\n", "phonebook.dat");
      reval = FAIL;
  }
  
  // write the entire array into the file
  irc = fwrite(phonearr, sizeof(phoneaddress), n, fp);
  printf(" fwrite return code = %d\n", irc);
  fclose(fp);
  
  //read from this file to array again
  if ((fp = fopen("phonebook.dat","rb")) == NULL){
      printf("Can not open %s.\n", "phonebook.dat");
      reval = FAIL;
      }
  irc = fread(phonearr, sizeof(phoneaddress), n, fp);
  printf(" fread return code = %d\n", irc);
  for (i=0; i&lt;n; i++){
      printf("%s\t\t",phonearr[i].name);
      printf("%s\t\t",phonearr[i].tel);
      printf("%s\n",phonearr[i].email);
  }
  fclose(fp);
  return reval;
  }
  </code></pre>
  
  <p>And the second program asks users how many contacts they would like to extract (which will be stored in phone1.dat) and the rest will be stored in phone2.dat. The problem is I don't know how to make 'the rest' to be stored in phone2.dat.</p>
  
  <p>Also anyone show me how to print out phone1 and phone2.dat on the screen? I'm very new to this, please :( </p>
  
  <p>My second program</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #define MAX 100
  typedef struct phoneaddress_t {
  char data[MAX];
  }phoneaddress;
  
  int main()
  {
  phoneaddress phonedat;
  FILE *f1 = fopen ("phonebook.dat", "r+b");
  FILE *f2 = fopen ("phone1.dat", "w+b");
  FILE *f3 = fopen ("phone2.dat", "w+b");
  char *ptr;
  int irc, size;
  
  printf("How many addresses do you want to extract? Enter: ");
  scanf("%d", &amp;size);
  
  if (f1 == NULL)
  {
      printf ("Cant open original phonebook.dat\n");
      return 1;
  }
  else if (f2 == NULL)
  {
      printf ("Cant open file phone1.dat\n");
      return 1;
  }
  else if (f2 == NULL)
  {
      printf ("Cant open file phone1.dat\n");
      return 1;
  } else {
      ptr = (phoneaddress*) malloc (size * sizeof(phoneaddress));
      if (ptr == NULL)
      {
          printf ("Memory allocation failed!\n");
          return 1;
      } 
  }
  fread (ptr, sizeof(phoneaddress), size, f1);
  fwrite (ptr, sizeof(phoneaddress), size, f2);
  
  printf("%s", ptr);
  
  fclose(f2);
  
  //fread (ptr, sizeof(phoneaddress), size, f1);
  //fwrite (ptr, sizeof(phoneaddress), size, f3);
  
  
  } 
  </code></pre>