Im making a database, where the information about books and readers in a library are contained in two linked lists, and each line of the txt file contains the data of one book/peopleinlibrary. Data of the books: id;year;title;writer;isborrowed;\n ...
The id and the isborrowed(1or0) are integers, the rest are strings. I know the atoi can convert the input lines to numbers, but when I use it while separating each line by commas, it just doesn't work, the printig is wrong and also i can't return with the *start/*begin pointer, where the list starts. By now im totally clueless because it's not the first method that I tried.
The part of the code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Konyv{
int id;
char *year;
char *title;
char *writer;
int ki;
struct Konyv*next;
}Konyv;
int main(){
Konyv*start=NULL;
FILE*f;
const char s[1] = ";";
int i;
f=fopen("konyv.adat.txt","r+");
Konyv*u;
if(f != NULL){
char line[1000];
while(fgets(line, sizeof line, f) !=NULL){
u= (Konyv*)malloc(sizeof(Konyv));
u->id = strtok(line, s);
u->id=atoi(u->id);
printf("%d ",u->id);
u->year = strtok(NULL,s);
printf("%s ",u->year);
u->title = strtok(NULL,s);
printf("%s ",u->title);
u->writer = strtok(NULL,s);
printf("%s ",u->writer);
u->ki = strtok(NULL,s);
u->ki=atoi(u->ki);
printf("%d",u->ki);
printf("\n");
u->next=NULL;
if(start==NULL){
start=u;
}
else{
Konyv *mozgo = start;
while (mozgo->next!= NULL){
mozgo = mozgo->next;
}
mozgo->next= u;
}
}
else{
printf("Error while opening.\n");
return 0;
}
printf("\n");
//test if start pointer is right by printig the list again(HELP)
Konyv* temp;
temp=start;
while(temp!=NULL) {
printf("%d ",temp->id);
printf("%s ",temp->year);
printf("%s ",temp->title);
printf("%s ",temp->writer);
printf("%d ",temp->ki);
printf("\n");
temp = temp->next;
}
free(u);
fclose(f);
return 0;
}