Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 30
void InputFile (char* input_file);
void OpenFile (char* input_file);
struct student
{
char *name;
int exam1;
int exam2;
int exam3;
int exam4;
int exam5;
char grade;
int average;
struct student *next;
};
typedef struct student student;
student* AddNode (char *name, int exam1, int exam2, int exam3, int exam4, int exam5, student *head);
student* StudentInfo(FILE *input_stream, int students_registered, int exam_number, student *head);
int main ()
{
char input_file[BUFSIZ];
InputFile(input_file);
OpenFile(input_file);
getchar();
getchar();
return 0;
}
void InputFile (char* input_file)
{
printf("Enter the name of the file containing the grades.\n");
scanf("%s", input_file); //Retrieving filename from user
return;
}
void OpenFile (char* input_file) //Collect necessary information from file
{
FILE *input_stream;
char* file = input_file;
int students_registered;
int exam_number;
student *head = NULL; //Creation of Head for Sorted Linked List
head = malloc(sizeof(student));
if ( (fopen(input_file, "r")) == NULL )
{
printf("Could not open file.\n\n");
return;
}
else
input_stream = fopen(input_file, "r");
//Figuring out how many students are registered and number of exams
fscanf(input_stream, "%d %d", &students_registered, &exam_number);
//Calling StudentInfo function to figure out rest of student information
head = StudentInfo(input_stream, students_registered, exam_number, head);
//puts(head->next);
printf("<%s %d>\n",head->name, head->name);
printf("%s %d %d %d\n", head->name, head->exam1, head->exam2, head->exam3); //Inside Main Function, where it messes up
fclose(input_stream);
return;
}
student* AddNode (char name[], int exam1, int exam2, int exam3, int exam4, int exam5, student *head)
{
int i;
student *node;
node = malloc(sizeof(student));
node->name = name;
node->exam1 = exam1;
node->exam2 = exam2;
node->exam3 = exam3;
node->exam4 = exam4;
node->exam5 = exam5;
//node->grade = grade; //Dont forget to do function
//node->average = average;
node->next = head;
head = node;
return head;
}
student* StudentInfo(FILE *input_stream, int students_registered, int exam_number, student *head)
{
int i, j;
char last_name[MAX_NAME];
char first_name[MAX_NAME];
char *name;
int exam[5];
char *p;
for(i = 0; i < students_registered; i++)
{
fscanf(input_stream, "%s", last_name);
fscanf(input_stream, "%s", first_name);
//Formatting name
strcpy(last_name + strlen(last_name), " ");
name = strcat(last_name, first_name);
for(j = 0; j < exam_number; j++)
{
fscanf(input_stream, "%d", &exam[j]);
}
switch (j)
{
case 1: head = AddNode (name, exam[0], -1, -1, -1, -1, head);
break;
case 2: head = AddNode (name, exam[0], exam[1], -1, -1, -1, head);
break;
case 3: head = AddNode (name, exam[0], exam[1], exam[2], -1, -1, head);
break;
case 4: head = AddNode (name, exam[0], exam[1], exam[2], exam[3], -1, head);
break;
case 5: head = AddNode (name, exam[0], exam[1], exam[2], exam[3], exam[4], head);
break;
}//End of Switch
}//End of for loop
printf("<%s %d>\n",head->name, head->name);
printf("%s %d %d %d\n", head->name, head->exam1, head->exam2, head->exam3); //Outside of main function
//printf("%d", &*head.name);
return head;
} //End of StudentInfo Function
By the way, I know I haven't freed the stuff yet and I will switch most of my input functions to fgets once I get it working. But fgets right now unnecessarily complicates things.