Esempio n. 1
0
void read_students(char student_file_name[], student **students, 
  int *student_size, int *student_count)
{
  *student_count = 0;
  char temp_buffer[100], *name, *temp_ptr = NULL;
  int class_count;
  FILE *student_file = fopen(student_file_name, "r");

  if(student_file == NULL)
    exit(1);

  initialize_students(students); 
  *student_size = 10;
  
  while(fgets(temp_buffer, 100, student_file) != NULL)
  {
    class_count = 0;
    name = strtok(temp_buffer, ",");
    (*students)[*student_count].last_name = (char*)malloc(strlen(name) + 1);
    strcpy((*students)[*student_count].last_name, name);
    name = strtok(NULL, ",");
    (*students)[*student_count].first_name = (char*)malloc(strlen(name) + 1);
    strcpy((*students)[*student_count].first_name, name);
    strcpy((*students)[*student_count].SID, strtok(NULL, ","));
    temp_ptr = strtok(NULL, ", \n");
    while(temp_ptr != NULL)
    {
      (*students)[*student_count].CRN[class_count] = atoi(temp_ptr);
      temp_ptr = strtok(NULL, ", \n");
      class_count++;
    }
    (*students)[*student_count].count = class_count; 
    (*student_count)++;
    
    if(*student_count == *student_size)
    { 
      resize_students(students, *student_size);
      *student_size *= 2;
    }//if
  }
  fclose(student_file);
}//read_courses
Esempio n. 2
0
void read_students(Student **students, int *count, int *size,
  const char *filename)
{
  char line[80], *ptr;
  FILE *fp = fopen(filename, "r");

  if(fp == NULL)
  {
    printf("%s not readable.\n", filename);
    exit(1);
  } // if file not openable
  
  *count = 0;
  initialize_students(students, size);

  while(fgets(line, 80, fp))
  {
    if(*count == *size)
      resize_students(students, size);

    ptr = strtok(line, ",");
    (*students)[*count].first_name = (char*) malloc(strlen(ptr) + 1);
    strcpy((*students)[*count].first_name, ptr);
    ptr = strtok(NULL, ",");
    (*students)[*count].last_name = (char*) malloc(strlen(ptr) + 1);
    strcpy((*students)[*count].last_name, ptr);
    strcpy((*students)[*count].SID, strtok(NULL, ",\n"));
    (*students)[*count].count = 0;

    while((ptr = strtok(NULL, ",\n")) != NULL)
      (*students)[*count].CRNs[(*students)[*count].count++] = atoi(ptr);

    (*count)++;

  } // while more in file

} // read_students()