Step 3
Step 3
Now let's take a look at contents in the datafile
data.txt. Its data maps (almost) directly to the fields
defined in the STUDENT struct:
In data.txt:
(TA's Name)
(Student ID)
(First Name)
(Last Name)
(Grade)
In linkedlist.h:
/* The STUDENT node will store info *
* about one student in a class. */
typedef struct student
{
char firstName[20];
char lastName[20];
int grade;
int id;
} STUDENT;
- Note that this step, 3a., is located at the bottom of the file,
in the ReadFile() function. Construct the linked list iteratively
by reading in the data for one student, creating a new node,
initializing the new node to be the data of the student just read in,
and then add the new node to the linked list identified by the TA's
name that you #defined. Remember that the return type of the
function fscanf is the number of items successfully
read, returning EOF when it reaches the end of the file.
Follow this pseudo code to write the actual code in lab12.c:
while (fscanf ( from the file into taName ) != EOF )
{
fscanf (from the file into tempStudent the data for all of its members)
Set the tempNode equal to the node created by a call to CreateNode()
Call SetData() to copy the tempStudent into the newly created tempNode
Examine the TA's name to Insert() the initialized tempNode into the
correct list.
}
- Back, higher up in the code, uncomment the call to ReadFile()
- Uncomment the three calls to PrintList() to see what's
in your linked lists.
Once your linked list's output matches what's printed below,
move on to Step 4
linux1[16]% gcc -Wall -ansi -c lab12.c
lab12.c: In function `main':
lab12.c:41: warning: unused variable 'id'
linux1[17]% gcc lab12.o linkedlist.o
linux1[18]% a.out data.txt
Welcome to the last lab of the semester! :)
David's Section :
First Name Last Name ID Grade
-------------------- -------------------- ------------ -------
Ann Lewis 123 98%
Mark Johnson 345 78%
Justin Lewis 122 99%
Bob Ramsey 141 64%
Julia's Section :
First Name Last Name ID Grade
-------------------- -------------------- ------------ -------
Joseph Eisenhower 212 82%
Gary Watson 532 92%
Jamie Casselman 621 100%
Claire Dishon 161 76%
Aafreen's Section :
First Name Last Name ID Grade
-------------------- -------------------- ------------ -------
Mary Jones 634 85%
Jenny Garcia 121 87%
Samantha Warrez 124 86%
linux1[19]%