So, this is my very small code which I was about to start but FOR AN UNKNOWN reason it crashes on scanf
. I checked with printf
and that's the place where it crashes. I've tried a lot of things and I can avoid this problem but I just want to know what's wrong if you could please tell me.
For example this can be datafile:
19/03/2017 Good
17/03/2017 Terrible
18/03/2017 Good
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DATA_FILE "Database.txt"
#define MAX_USERS 50
typedef struct data {
char date;
char feeling;
} data;
int ReadData(data *p1, FILE *fp);
int main() {
FILE *fp = fopen(DATA_FILE, "r");
data *p1;
p1 = (struct data*)malloc(MAX_USERS * sizeof(struct data));
if (fp == NULL) {
printf("Database is empty!");
exit(1);
}
ReadData(p1, fp);
}
int ReadData(data *p1, FILE *fp) {
int i = 0;
while (!feof(fp)) {
fscanf(fp, "%s %s", (p1 + i)->date, (p1 + i)->feeling);
printf("%s %s", &(p1 + i)-> date, &(p1 + i)-> feeling);
i++;
}
return i;
}