I have not given up. I don't give up THAT easily but am frustrated beyond imagination. Okay, I put the name arrays in the loop, here's where I am at:
Code:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define MAX_CHARS_IN_NAME 30
typedef struct
{
char *first_name;
char *last_name;
char month[10];
int day;
int year;
} person;
int main()
{
// Create a file pointer and open the file
FILE *ifp = fopen("birthday.txt", "r");
// Test to see if the file opens
if (!ifp)
{
printf("The file is unable to open!\n");
return -1;
}
// Create variables for the number of classes, number of students in the class, birthday month/day/year, number of queried students, queried students
int i, num_classes, num_students, num_of_queried;
char queried_student;
// Read in the number of classes. PROPERLY READ IN (TESTED)
fscanf(ifp, "%d", &num_classes);
// Read in the number of students in the class. PROPERLY READ IN (TESTED)
fscanf(ifp, "%d", &num_students);
// Allocating space for the students in the classroom
person *student = malloc(sizeof(person)*num_students);
/*
// Allocating space for the queried student
person *queried_student = malloc(sizeof(person));
queried_student->first_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
queried_student->last_name = malloc(sizeof(char) *MAX_CHARS_IN_NAME);
*/
// Loop through the text file
for (i=0; i< num_classes; i++)
{
int j;
for (j=0; j< num_students; j++)
{
student->first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
student->last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
printf("j = %d\n", j);
// Scan in the student's first name
fscanf(ifp, "%s", student[j].first_name);
printf("student[%d]->first_name = %s\n", j, student->first_name);
// Scan in the student's last name
fscanf(ifp, "%s", student[j].last_name);
printf("student[%d]->last_name = %s\n", j, student->last_name);
// Scan in the student's birth month
fscanf(ifp, "%s", student[j].month);
printf("student[%d]->month = %s\n", j, student->month);
// Scan in the student's birth day
fscanf(ifp, "%d", &student[j].day);
printf("student[%d]->day = %d\n", j, student->day);
// Scan in the student's birth year
fscanf(ifp, "%d", &student[j].year);
printf("student[%d]->year = %d\n", j, student->year);
// Scan in the number of queried students
//fscanf(ifp, "%d", &num_of_queried);
/* int k;
for (k=0; k< num_of_queried; k++)
{
} */
}
}
// Sort the list in order of birthday, IGNORING THE YEAR!!!
// print output
fclose(ifp);
return 0;
}
Also tried this:
Code:
for (i=0; i< num_classes; i++)
{
int j;
for (j=0; j< num_students; j++)
{
// Allocating space for the students in the classroom
person *student = malloc(sizeof(person)*num_students);
student->first_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
student->last_name = malloc(sizeof(char) * MAX_CHARS_IN_NAME);
printf("j = %d\n", j);
// Scan in the student's first name
fscanf(ifp, "%s", student[j].first_name);
printf("student[%d]->first_name = %s\n", j, student->first_name);
// Scan in the student's last name
fscanf(ifp, "%s", student[j].last_name);
printf("student[%d]->last_name = %s\n", j, student->last_name);
// Scan in the student's birth month
fscanf(ifp, "%s", student[j].month);
printf("student[%d]->month = %s\n", j, student->month);
// Scan in the student's birth day
fscanf(ifp, "%d", &student[j].day);
printf("student[%d]->day = %d\n", j, student->day);
// Scan in the student's birth year
fscanf(ifp, "%d", &student[j].year);
printf("student[%d]->year = %d\n", j, student->year);
// Scan in the number of queried students
//fscanf(ifp, "%d", &num_of_queried);
/* int k;
for (k=0; k< num_of_queried; k++)
{
} */
}
}
If it matters, my text file is:
Code:
2
3
JOHN SMITH MARCH 7 1968
PETER THOMPSON MAY 12 1977
STEVEN WILSON FEBRUARY 18 1959
2
JOHN SMITH
STEVEN WILSON
4
DANIEL MARNER MAY 12 1968
JOHNSON ELLINGTON JUNE 28 1960
BILLY CLINT AUGUST 10 1950
PETER FOUTS MARCH 10 1951
1
BILLY CLINT