title: Why do strange characters appear when I attempt to write to file?
body: <p>In this program I attempt to write a file that displays an unknown number of records entered by the user. Each record has the following fields: First Name, Last Name, Address, City, State, Zip Code, and Phone Number. I assumed the best way to do this would be to define a struct Record with the fields above, then declare an array of Records that would contain as many records as the user entered. To accomplish this I would use a loop to get the inputs for each field per record, then if the user wanted to continue dynamically allocate an extra space in the Record array and continue until the user enters no. The program compiles and creates the file, but the characters written are totally weird. What causes this to happen?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;conio.h&gt;
  
  struct Record
      {
          char fname[51];
          char lname[51];
          char address[51];
          char city[51];
          char state[51];
          int zipcode;
          int phoneNumber;
      };
  
  
  
  
  int main()
  {
      FILE *fileWriter;
      const char filename[] = "data.txt";
      char answer = 'y';
      int size = 1;
      int i = 0;
      struct Record *records = NULL;
      struct Record *records_temp;
  
  
  
  
      while(answer == 'y' || answer == 'Y')
      {
          struct Record *records_temp = realloc(records,(size)*sizeof(*records));
  
          if(records_temp == NULL)  
          {
              free(records); 
  
          }
          records = records_temp;
          printf("First Name: \n");
          scanf("%s", records[i].fname);
          printf("Last Name: \n");
          scanf("%s", records[i].lname);
  
          printf("Address: \n");
          scanf(" %[^\n]", records[i].address);
  
          printf("City: \n");
          scanf("%s", records[i].city);
  
          printf("State: \n");
          scanf("%s", records[i].state);
  
          printf("Zipcode: \n");
          scanf("%d", &amp;records[i].zipcode);
  
          printf("Phone Number: \n");
          scanf("%d", &amp;records[i].phoneNumber);
          //stores all record info
  
          printf("Are there anymore records? [y/n] ");
          answer = getchar();
          if(answer == 'y' || answer == 'Y')
          {
              size++;
              records[i++];
              printf("\n");
          }
  
          //open file
  
      fileWriter = fopen(filename,"wb");
  
      if(fileWriter != NULL)
      {
          if(fwrite(records,sizeof(*records),size,fileWriter) != 1)
          {
              fprintf(stderr, "Failed to write to %s\n", filename);
              exit(1);
          }
          fclose(fileWriter);
      }
      else
      {
          printf("Error opening file.");
      }
  }
  }
  </code></pre>
  
  <p><strong>EDITED VERSION</strong>
  I've modified the code and fixed many of the errors that were occurring, but now I am currently getting an access violation at  scanf( "%c", answer)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;conio.h&gt;
  
  struct Record
      {
          char fname[51];
          char lname[51];
          char address[51];
          char city[51];
          char state[51];
          char zipcode[51];
          char phoneNumber[51];
      };
  
  
  
  
  int main()
  {
      FILE *fileWriter;
      const char filename[] = "data.txt";
      char answer = 'y';
      char a = 'n';
      int size = 1;
      int i = 0;
      struct Record *records;
      struct Record *records_temp = NULL;
  
  
  
  
      while(answer == 'y' || answer == 'Y')
      {
          struct Record *records_temp = calloc((size),sizeof(*records));  
  
          records = records_temp;
          printf("First Name: \n");
          scanf("%s", records[size-1].fname);
  
          printf("Last Name: \n");
          scanf("%s", records[size-1].lname);
  
          printf("Address: \n");
          scanf(" %[^\n]", records[size-1].address);
  
          printf("City: \n");
          scanf("%s", records[size-1].city);
  
          printf("State: \n");
          scanf("%s", records[size-1].state);
  
          printf("Zipcode: \n");
          scanf("%s", records[size-1].zipcode);
  
          printf("Phone Number: \n");
          scanf("%s", records[size-1].phoneNumber);
          //stores all record info
  
          printf("Are there anymore records? [y/n] ");
          scanf(" %c", answer);
          if(answer == 'y' || answer == 'Y')
          {
              size++;
              printf("\n");
          }
      }
          //open file
  
      fileWriter = fopen(filename,"wb");
  
      if(fileWriter != NULL)
      {
          for(;i&lt; size; i++)
          {
              fprintf(fileWriter,"%s\n",records[i].fname);
              fprintf(fileWriter,"%s\n",records[i].lname);
              fprintf(fileWriter,"%s\n",records[i].address);
              fprintf(fileWriter,"%s\n",records[i].city);
              fprintf(fileWriter,"%s\n",records[i].state);
              fprintf(fileWriter,"%d\n",records[i].zipcode);
              fprintf(fileWriter,"%d\n",records[i].phoneNumber);
          }
          fclose(fileWriter);
      }
      else
      {
          printf("Error opening file.");   
      }
  
  }
  </code></pre>