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
Code:
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
and the code ive written so far
Code:
#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);
}
i compiled it and this is what is displayed
Name? input.txt
Name of Movie:Meet the Parents - Rating:= - Date: - Length:0
also how would i go about doing it for the nextline as this is a test for only one line