Hello!
I need to read text from a file that is formatted like this:
artist;album;release type;rating
and then save it to a struct that looks like this:
Code:
typedef struct {
char artist[41];
char album[41];
char type[3];
char rating[5];
} record;
The problem is that I don't really now if I want to use fscanf or fgets and how to put the info in the struct. I also want to put the struct to a list but that's easier I think.
Anyway here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
#define BUFSIZE 90
typedef struct {
char artist[41];
char album[41];
char type[3];
char rating[5];
} record;
void readFile(FILE *infilep, dlist *list);
int main(int argc, char *argv[]) {
FILE * infilep;
infilep = fopen(argv[1],"r");
if (infilep == NULL) {
fprintf(stderr, "Couldn't open input file.");
exit(1);
}
if(argc < 1) {
fprintf(stderr, "Usage: %s <musik.txt>\n", argv[0]);
exit(1);
}
dlist *list=dlist_empty();
dlist_setMemHandler(list, free);
readFile(infilep, list);
return 0;
}
void readFile(FILE *infilep, dlist *list) {
char line[BUFSIZE];
dlist_position pos;
pos = dlist_first(list);
while (fgets(line, BUFSIZE, infilep) =! NULL) {
record *info=malloc(sizeof(record));
fgets(infilep, "%41[^;]%41[^;]%3[^;]%5[^;]",
&info->artist, &info->album,
&info->type, &info->rating);
dlist_insert(list, pos, record);
dlist_next(list, pos);
}
}
Help appreciated!
Thanks