I'm trying to read my linked list from file with no luck. Struct is properly saved with fprintf (couldn't get it to work with fwrite). Here is my struct:
typedef struct database
{
int id;
int nrindeksu;
char *imie;
char *nazwisko;
char *wydzial;
char *kierunek;
struct database* link;
} data;
char buffer[1024]; //changed buffer size
I'm saving linked list to file using this function: Edit: After some changes to other functions this one crashes.
void save(data* head)
{
FILE *fp;
fp = fopen("test.txt", "w");
data* current = head->link;
while (current != NULL) {
fprintf(fp,"%d ",current->nrindeksu);
fprintf(fp,"%s ",current->imie);
fprintf(fp,"%s ",current->nazwisko);
fprintf(fp,"%s ",current->wydzial);
fprintf(fp,"%s\n",current->kierunek);
current = current->link;
}
fclose(fp);
}
and trying to read it from file with this:
void read(data* head)
{
data* current = head;
FILE *fp;
fp = fopen("test.txt", "r");
while(//I still need help with this loop)
fscanf(fp, "%d", current->link->nrindeksu);
fscanf(fp, "%s", buffer);
current->link->imie=malloc(strlen(buffer) + 1);
if(current->link->imie == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->imie, buffer);
fscanf(fp, "%s", buffer);
current->link->nazwisko=malloc(strlen(buffer) + 1);
if(current->link->nazwisko == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->nazwisko, buffer);
fscanf(fp, "%s", buffer);
current->link->wydzial=malloc(strlen(buffer) + 1);
if(current->link->wydzial == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->wydzial, buffer);
fscanf(fp, "%s", buffer);
current->link->kierunek=malloc(strlen(buffer) + 1);
if(current->link->kierunek == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->kierunek, buffer);
fclose(fp);
}
Any idea why it doesn't read anything? Also I don't know how to code a loop that will determine where it should stop reading from file.
Edit: I'm using this function to add items to the list:
void add_bottom(data* head)
{
data* current = head;
while (current->link != NULL)
{
current = current->link;
}
current->link = malloc(sizeof(*current));//changed every malloc
if(current->link == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
printf("Podaj nr indeksu studenta: ");
scanf("%d", current->link->nrindeksu);
printf("Podaj imie studenta: ");
fflush(stdin);
scanf("%19[^\n]", buffer);
current->link->imie=malloc(strlen(buffer) + 1);
if(current->link->imie == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->imie, buffer);
printf("Podaj nazwisko studenta: ");
fflush(stdin);
scanf("%19[^\n]", buffer);
current->link->nazwisko=malloc(strlen(buffer) + 1);
if(current->link->nazwisko == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->nazwisko, buffer);
printf("Podaj wydzial studenta: ");
fflush(stdin);
scanf("%29[^\n]", buffer);
current->link->wydzial=malloc(strlen(buffer) + 1);
if(current->link->wydzial == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->wydzial, buffer);
printf("Podaj kierunek studenta: ");
fflush(stdin);
scanf("%29[^\n]", buffer);
current->link->kierunek=malloc(strlen(buffer) + 1);
if(current->link->kierunek == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
strcpy(current->link->kierunek, buffer);
current->link->link = NULL;
}
Main:
int main(int argc, char *argv[])
{
char c;
head = init(head);
if(head == NULL) {
fprintf(stderr, "Malloc error");
exit(0);
}
read(head);
do{
printf("Baza danych - wybierz opcje: [d]odaj, [w]yswietl, [u]sun, [k]oniec: ");
scanf("%c", &c);
if(c=='d')
{
system("cls");
add_bottom(head);
}
if(c=='w')
{
system("cls");
show(head);
}
if(c=='u')
{
system("cls");
show(head);
remove_by_id(&head);
system("cls");
show(head);
printf("\n");
}
fflush(stdin);
}while(c!='k');
save(head);
free(head);
return 0;
}
Also function init is just
head = calloc(1,sizeof(data));
This way first node is always NULL, couldn't get it to work other way to create or edit first node.