0

[I write the program in C language]

I would like to read a txt file containing StudentName, Score, and Remarks

The pattern of the txt file looks like this:

1,Adam Lambert,60,C

2,Josh Roberts,100,A

3,Catherine Zetta,80,B

I would like to store each data, respectively into an array

So I would have 3 arrays (to store StudentName, Scores, and Grades)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>

char* studName[99];
char* grade[99];
char* grades[100][10];
char buff[1024];
int scores[100];
int score;
int index;
int size;
int index = 0;
int sum = 0;
double average = 0;

int main()
{

    FILE *file;
    file = fopen("notepad.txt", "r");

    if(file == NULL){
        printf("Data does not Exist");
    }
    else {

        while(((fgets(buff,1024,file))!=NULL))
        {
            size = strlen(buff);
            buff[size] = 0;
            sscanf(buff, "%d, %[^,],%d, %[^,]", &index, studName,&score, grade);

            printf("Student Name: %s\n", studName);
            printf("Score: %d\n", score);
            printf("Remark: %s\n", grade);

            scores[index-1] = score;
            grades[index-1][10] = grade;
            sum+=score;
            index++;

        }

    }
     fclose(file);


        average = sum/index;
        printf("\nThe total score of student is: %d\n", sum);
        printf("\nThe sum of student is: %d\n", index);
        printf("\nThe average of students' score is: %2f\n", average);

        for(int i = 0; i<3; i++){

            printf("Score: %d\n", scores[i]);
            printf("\nThe Remark is: %s\n", grades[i][10]);
        }
     getch();
    return 0;
}

The code above had successfully stored the scores in int array. I had not really good in C, so I do not know how to store the char array for StudentName and Grades.

The abovde code gives the result of Grades stored is only the last grade on the txt file (in this case is 'B').

Would you please tell me what do I have to fix in order to achieve my goal?

My goal basically will be pretty much like this:

StudentName array contains {"Adam Lambert", "Josh Roberts", "Catherine Zetta"}

Scores array contains {60,100,80}

Grades array contains {"C", "A", "B"}

Thank you very much.

Raymond
  • 604
  • 1
  • 9
  • 27

2 Answers2

0

Try this:

 float len=sizeof(string);

Just gives you the static size of the array string which is an array of 20 pointers and each pointer is 4 bytes, 20 * 4 bytes = 80 that's why it's always 80.

while (fgets(string[i], BUFSIZE, fp)) {
    i++;
    len+=strlen(string[i]);
    string[i] = (char *)malloc(BUFSIZE);    
} 

If your recore size is fixed then u can use this: Where fr is a file pointer

fscanf(fr, "Index: %d StuName: %s Score: %d Grade: %s ", &index, sname, &score, grade);
KethanKumar
  • 716
  • 5
  • 17
0

Change studName and grade from "char *" to "char". You want a buffer, not an array of pointers.

At the top add: char * StudentName[100];

When setting "scores" and "grades" add:

StudentName[index-1] = strdup(studName);

Also change the "10" in the following line, the array only dimensions 0-9. grades[index-1][10] = grade;