Hello,
I've a file which contains names and grades of students, and I'd like to write a program which can sort their grades (like midterm 1,midterm 2) according to user choice. I wrote as far as the choice part and opening the file, yet I don't know how to make program read only certain part of the file (like only Midterm 1 grades for example) and sort them only,then need to define a structure named “student”(so I can store name,surname and scores of each student under the same variable name)At the end, all data in the file needs to be stored in an array of new data type named “student”. Here's what I've wrote so far;

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int choice,studentnumber,midterm1,midterm2,midterm3;
    char name[30];
    char surname[30];
    FILE *cfPtr;

    if ((cfPtr = fopen("grades.txt", "r")) == NULL)
    printf("Dosya açılamadı.\n");
    else {
         printf("%s%s%s%s\n", "StudentNumber", "Name", "Surname", "Midterm1", "Midterm2", "Midterm3");
         fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);

         while (!feof(cfPtr)) {
               printf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);
               fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);

}
    printf("What would you like to do? \n"
    "1- Sort according to midterm 1\n"
    "2- Sort according to midterm 2\n"
    "3- Sort according to midterm 3\n"
    "4- Exit\n"
    scanf("%d",&choice);

    while (choice != 4);{
    fprintf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);

    switch(choice) {


    fclose(cfPtr);
}

  system("PAUSE");  
  return 0;
}

After those, I have absolutely no idea what to do. Any ideas to give me? Thanks :)

Recommended Answers

All 12 Replies

First, create the structure and include all the grades (and all the data) from the file.*
Create the read function to read a student record.
Next create a write function that displays the data for 1 student.
Write the main function to:
1) read all students
2) display all students

Once you have that, you can then move on to the rest of the project.

* By including all the data, you can expand the program to do other things as time goes by. Simply add another function or two and a new menu entry.

As WaltP said. In addition, once you read all of the student records into an array, you can sort it as you wish using the qsort() function / algorithm. You just need to specify the comparison function to use to determine which record is to be sorted ahead of another. See the Linux/Unix man pages for qsort() and bsearch().

My problem is, I really don't know how to read the records into an array. I started with the first part;

int choice,studentnumber,midterm1,midterm2,midterm3;
   char name[30];
   char surname[30];
   FILE *cfPtr;

which was actually meant to be struct going into array part, yet failed. Any guidence with that? Sorry I'm a starter, and a really bad one :D. Thanks.

What you posted is not a failure. It a lack of attempt.

Read your text book about defining a structure.

I did read of course, but the thing is, in the examples they're always writing the info before hand. I couldn't find any (proper) C examples from reading a file, then putting that info into struct/array.

I don't understand what that means.

1) You create the date file.
2) You write a program to read the data file and put the data into your struct/array

Okay this is what I've come to so far:

#include <stdio.h>
#include <stdlib.h>

    typedef struct {
    int studentnumber;
    char name[30];
    char surname[30];
    int midterm1,midterm2,midterm3;
    } Student;

int main()
{
    int choice,studentnumber,midterm1,midterm2,midterm3;
    FILE *cfPtr;

    struct student *name;
    name = malloc( 10 * sizeof(Student));

    if ((cfPtr = fopen("grades.txt", "r")) == NULL)
    printf("Dosya açılamadı.\n");
    else {
         printf("%s%s%s%s\n", "StudentNumber", "Name", "Surname", "Midterm1", "Midterm2", "Midterm3");
         fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);

         while (!feof(cfPtr)) {
               printf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);
               fscanf(cfPtr, "%d%s%s%d%d%d", &studentnumber, &name, &surname, &midterm1, &midterm2, &midterm3);

}
    printf("What would you like to do? \n"
    "1- Sort according to midterm 1\n"
    "2- Sort according to midterm 2\n"
    "3- Sort according to midterm 3\n"
    "4- Exit\n"
    scanf("%d",&choice);

    while (choice != 4);{
    fprintf("%4d%15s%15s%10d%10d%10d\n", studentnumber, name,surname, midterm1, midterm2, midterm3);




    fclose(cfPtr);
}

  system("PAUSE");  
  return 0;
}

I am still clueless about how to put struct into array, without actually writing all the data in the file to the program and making it read by itself.

You don't put the struct into an array. You make the structure an array:

typedef struct 
{
    int studentnumber;
    char name[30];
    char surname[30];
    int midterm1,midterm2,midterm3;
} Student[30];

As for:

writing all the data in the file

just create a file using any editor (like NotePad) that contains the data you want to read.

I already have a file, but the thing I mean is, do I have to hard code the info into the code? Or how can the program get it automatically?

Okay this is what i've come up with so far;

#include <stdio.h>
#include <stdlib.h>

    typedef struct {
    int number;
    char name[30];
    char surname[30];
    int midterm1,midterm2,midterm3;
    } Student;

    int comp(const int * a, const int * b) 
{
  if (*a==*b)
    return 0;
  else
    if (*a < *b)
        return -1;
     else
      return 1;
}

int main()
{
    int choice,studentnumber,midterm1,midterm2,midterm3;
    char surname;
    FILE *cfPtr;

    struct student *name;
    name = malloc( 10 * sizeof(Student));

    if ((cfPtr = fopen("grades.txt", "r")) == NULL)
    printf("File cannot be opened.\n");
    else {


    const int STUDENTSMAX = 100;

    Student students[STUDENTSMAX];
    int i = 0;

    while (!feof(cfPtr))

    {
    fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
    printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
  i++;
}

    printf("What would you like to do? \n"
    "1- Sort according to midterm 1\n"
    "2- Sort according to midterm 2\n"
    "3- Sort according to midterm 3\n"
    "4- Exit\n");
    scanf("%d",&choice);

    while (choice != 4);{


      switch (choice) {

           case 1:
                qsort(students,10,sizeof(int),comp);
                for (i=0; i<9; i++)      
    printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1);
}




    fclose(cfPtr);
}

  system("PAUSE");  
  return 0;
}

Something's wrong with my qsort though.

and this is the error : "63 C:\Users\UseR\Desktop\main.c [Warning] passing arg 4 of `qsort' from incompatible pointer type". "
It also gives those errors too: "75 C:\Users\UseR\Desktop\main.c syntax error at end of input " " C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1 " "

and this is the error : "63 C:\Users\UseR\Desktop\main.c [Warning] passing arg 4 of `qsort' from incompatible pointer type". "

What does qsort() expect as a 4th parameter? What type of value?
Is comp one of those or is it something else?

It also gives those errors too: "75 C:\Users\UseR\Desktop\main.c syntax error at end of input " " C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1 " "

Reformat your program so its formatting is consisent, not haphazard. That usually solves this type of error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.