Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Student marks system using C

Status
Not open for further replies.

Tripswitch

New Member
I need help manipulating data from a .txt file as attached. As you can see there is 80 lines and 4 subjects which means there's 20 students.

For the first task I'm asked to show a table with a list of the 20 student IDs and their marks next to their ID.

Example:

868717 2 37 49 15
472832 44 88 91 95
.
.
.

If you look at my code the .txt file is read in using fscanf() and the ID numbers do not get read in as integers because it only works if they're read in as strings. The way fscanf() has read the data into my structure there's 80 IDs where there's 20 lots of 4 of the same which is causing some difficulties with my program.

What I'm trying to do is get the strcmp() function to "lock on" on to the identical IDs for each student and print their marks but it's not working out. The program compiles if you copy and paste it and you'll see what the output looks like. Don't worry about the beginning of the code I'll get rid of magical numbers and dynamically allocate things later.

Can anyone provide some advice, hints or how I can improve what I'm trying to do to get this to work.
Below is my code:

Code:
#include<stdio.h>
#include<string.h>
#defineSIZE 100
structmarks
{
char student_ID[80][SIZE];
char subject[80][SIZE];
int mark[80][SIZE];
}student;
int main(void)
{
FILE *input_file;
int i, j, k, data_items;
if((input_file=fopen("C:\\file\\file.txt", "r"))==NULL)
perror("File open failed:");
else
{
for(i=0;i<80;i++)
{
while((data_items=fscanf(input_file, "%s %s %d", student.student_ID[i], student.subject[i], student.mark[i])!=3));
}
fclose(input_file);
}
for(j=0; j<80; j++)
{
for(k=0; k<80; k++)
{
if(strcmp(student.student_ID[j], student.student_ID[k])==0)
printf("%s %d \n", student.student_ID[k], *student.mark[k]);
}
}
return 0;
}
 

Attachments

  • file.txt
    1.6 KB · Views: 259
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top