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:
(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;
- 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, then add the new
node to the linked list. 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 tempStudent ) != EOF)
{
Set the tempNode equal to the node created by a call to CreateNode()
Call SetData() to copy the tempStudent into the newly created tempNode
Call Insert() to add the initialized tempNode into the list
}
- Uncomment the call to ReadFile and then call PrintList() to see your linked list.
Once your linked list's output matches what's printed below,
move on to Step 4
linux1[1]% gcc -Wall -ansi -c lab12.c
lab12.c: In function `main':
lab12.c:35: warning: unused variable 'id'
linux1[2]% gcc lab12.o linkedlist.o
linux1[3]% a.out data.txt
Welcome to the last lab of the semester! :)
First Name Last Name ID Grade
-------------------- -------------------- ------------ -------
Ann Lewis 123 98%
Joseph Eisenhower 212 82%
Mary Jones 634 85%
Mark Johnson 345 78%
Justin Lewis 122 99%
Jenny Garcia 121 87%
Gary Watson 532 92%
Jamie Casselman 621 100%
Samantha Warrez 124 86%
Bob Ramsey 141 64%
Claire Dishon 161 76%
linux1[4]%