I have a task, I am working with few days already, I am going crazy, Google doesn't help, only choice is to ask here.
I am a total beginner in C.
I have a file, ordinary .txt file.
Mathematics 5 1 2 3 4 5
Physics 6 1 2 3 4 5 6
Design 7 1 2 3 4 5 6 7
First word is a "course", first number is the amount of grades that are in this course, for example Math, first number is 5 and I have 5 grades, 1 2 3 4 5, same with other courses.
I need to create 3 different arrays.
Array of courses ("Mathematics", "Physics", "Design") and of course not with hands, but by taking all of these from FILE.
Array of amount of grades (First number on each line),
Array of average grades (All numbers except the first one on each line),
MY MAIN PROBLEM:
I can't divide my .txt file so that I get only string (Mathematics, Physics and Design).
Here is my code, most logical things I came up with, but fscanf unfortunately tells me that I can't convert STRING to INTEGER.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main () {
FILE *fp;
fp = fopen("C:\\Project\\project.txt", "r"); //opening already created file
int grades[500];
char courses[300];
for (int i = 0; i < 300; ++i) {
fscanf(fp, "%s", &courses[i]);
if(isdigit(courses[i]))
fscanf(fp, "%d", &grades[i]);
}
fclose(fp);
return(0);
}
Basically this code doesn't work. Program thinks about text in the file as String, I try to take it out as char and later send it to its respective arrays, but I physically can't do it.
Once again, First I need to take those course names, "Mathematics", "Physics" and "Design" as string and later to move on to numbers.
Thanks in advance