1

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.

  1. Array of courses ("Mathematics", "Physics", "Design") and of course not with hands, but by taking all of these from FILE.

  2. Array of amount of grades (First number on each line),

  3. 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

  • when the program iterates through your file, your very first fscanf successfully retrieves the course, then checks the succeeding values. so once your loop iterates again, then the fscanf will check on the digit side, and you are fetching it as a string... might want to consider having a fixed length grade to scan only once with "%s %d %d %d %d %d ..." – arjayosma Nov 14 '18 at 04:09
  • 1
    The best thing to do is see one of the teacher's assistants. You need a lot more help than can be provided in a stack overflow Q&A. – user3386109 Nov 14 '18 at 04:14
  • 1
    A hint - Take it line by line. For the first line you first scan the Name - put that in the courses array. Then scan the amount of grades. Put that in amount of grades array. Then run a loop for no of grades (scanned previously, say n). Scan n grades. Calculate the average. Put the average in the average array. Repeat entire thing 3 times for 3 lines (or until end of file) – Rishikesh Raje Nov 14 '18 at 04:24

2 Answers2

0

fscanf(, "%s", buffer) reads a string until a whitespace and puts the result in the buffer.

First call fscanf(fp, "%s", courses); will read Mathematics and store the word in courses array. Second call fscanf(fp, "%s", courses); will read 5 and store the character in courses array

You will need to convert characters to numbers, check for newlines, handle errors - what if an incoming character is not a digit, etc?

Your first call fscanf(fp, "%s", &courses[i]); gets a course name. Second iteration of your loop will give you "M1thematics" in the courses. You don't need [i] part. You need to change your loop. And what's that arbitrary 300 number?

Another hint: &courses[0] points to the start of the array, same as just courses. &courses[1] points to the second element in courses.

The rest is up to you.

dmitri
  • 3,183
  • 23
  • 28
0

You can solve this problem by reading the file line by line using fgets(),and extracting each word in line using strtok().strtok can separate line into words if we give delimiter as space.Below i am giving a code to extract courses and total_number of grades in my.txt to separate array. An unrefined Code written by me is given below.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void print_word (char *str ,int i);
static char content[10][40];
char courses[10][20];
int total_num_grades[10];
int line_count =0 ;

void parse_word (char *str ,int line_num)
{
        char *temp ;
        int word_num = 0;
        printf ("Splitting string \"%s\" into tokens:\n",str);
        temp  = strtok (str," "); //taking first word of line
        strcpy(courses[line_num] ,temp);
        temp = strtok (NULL, " "); //taking second word of line
        total_num_grades[line_num] = atoi(temp);


}

void print_result()
{
        printf("courses in the text \n\n");
        for(int i = 0; i <line_count ; i ++)
        {
                printf("%s\n",courses[i]);
        }
        printf ("\n\nnumber of grade array \n\n");
        for(int i = 0;i < line_count ;i ++)
        {
                printf("%d\n",total_num_grades[i]);
        }
}

int main () {
        FILE *fp;
        int len = 0,read = 0;
        fp = fopen("my.txt", "r");
        while ((fgets(content[line_count], 400, fp))) {
                printf("%s", content[line_count]);
                line_count ++;
        }
        for (int line_num = 0; line_num < line_count ; line_num++)
        {
         parse_word(&content[line_num][0],line_num);

        }
        print_result();
        return(0);
}

strtok usage example

coddygeek
  • 125
  • 1
  • 12