I have the following program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
typedef struct {int key; char data[MAXLEN];} record;
main(int argc, char *argv[])
{
int n, i;
record x;
FILE *fp;
fp = fopen(argv[1], "w+");
printf("How many records will be entered? \n");
scanf("%d", &n);
for (i=0; i<n; i++)
{
printf("Enter record: \n");
scanf("%d", &x.key);
scanf("%s", &x.data);
fwrite(&x, sizeof(record), 1, fp);
}
}
What I am doing is creating records from user input, and then storing these "records" into a file. However, when I use fwrite(), the file that is created has a lot of strange characters written in it, instead of simply having each record with its key and data. Can anyone tell me why it's writing all of these strange characters?