hi im trying to read the movie title, rating, date and length from a file and tryingto store it into an array of structs here is how the data file is
and the code ive written so farCode:Meet the Parents,PG,03/06/2001,108 Meet the Fockers,PG-13,04/19/2005,116 Fantastic Four,PG-13,07/10/2005,99 Harry Potter: Prisoner of Azkaban,Not-Rated,11/23/2004,142 Hitch,PG-13,07/02/2005,103
i compiled it and this is what is displayedCode:#define SIZE 10 struct{ char title[30]; char rating[40]; char date[40]; int length; } movies[SIZE]; int main() { int i; char name[100]; /* name of the file to use */ FILE *in_file; /* file for input */ printf("Name? "); fgets(name, sizeof(name), stdin); name[strlen(name)-1] = '\0'; in_file = fopen(name, "r"); if (in_file == NULL) { fprintf(stderr, "Could not open file\n"); exit(8); } fscanf(in_file, "%[^,]s,%[^,]s,%[^,]s,%d", &movies[1].title, &movies[1].rating,&movies[1].date,&movies[1].length); fclose(in_file); printf("Name of Movie:%s - Rating:%s - Date:%s - Length:%d",movies[1].title, movies[1].rating,movies[1].date,movies[1].length); getch(); return (0); }
also how would i go about doing it for the nextline as this is a test for only one lineName? input.txt
Name of Movie:Meet the Parents - Rating:= - Date: - Length:0



LinkBack URL
About LinkBacks


