Skip to content

Instantly share code, notes, and snippets.

@Experiment5X
Created May 5, 2013 18:40
Show Gist options
  • Save Experiment5X/5521728 to your computer and use it in GitHub Desktop.
Save Experiment5X/5521728 to your computer and use it in GitHub Desktop.
Some trash
#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>
struct _Student
{
LIST_ENTRY(_Student) entries;
char name[20];
int age;
float gpa;
} _Student;
int main()
{
FILE *myFile = fopen("/home/adam/Desktop/students.txt", "r");
if (myFile == NULL)
{
printf("An error occurred while opening the file.\n");
return -1;
}
// initialize the list
LIST_HEAD(listhead, _Student) studentsHead;
struct listhead *studentsp;
LIST_INIT(&studentsHead);
// read in all of the students
while (1)
{
struct _Student *tempStudent = malloc(sizeof(struct _Student));
int status = fscanf(myFile, "%s %i %f", tempStudent->name, &tempStudent->age, &tempStudent->gpa);
// check for end of file
if (status != 3)
break;
// add this new student to the list
LIST_INSERT_HEAD(&studentsHead, tempStudent, entries);
}
// traverse the list
int studentCount = 0;
struct _Student *itemS;
for (itemS = studentsHead.lh_first; itemS != NULL; itemS = itemS->entries.le_next, studentCount++)
printf("%s\t%i\t%.2f\n", itemS->name, itemS->age, itemS->gpa);
printf("Total of %i students.\n", studentCount);
// cleanup
fclose(myFile);
while (studentsHead.lh_first != NULL)
LIST_REMOVE(studentsHead.lh_first, entries);
return 0;
}
/* The input file looked like this:
Adam 17 94.0
D-Dog 17 74.2
Hanna 20 87.7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment