So I've gotten to the point where my data is being read in correctly in my getData function, but it crashes when I try to create the linked list..
Code:
/* Test driver for list functions.
Written by:
Date:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GPA_DS "gpaDS.txt"
#define MAX_SIZE 1000
// Global Declarations
typedef struct
{
char pin[4];
char *name;
double gpa;
} STU;
typedef struct nodeTag
{
STU data;
struct nodeTag* link;
} NODE;
// Function Declarations
NODE* insertNode (NODE* pList, NODE* pPre, STU item);
int searchList (NODE* pList, NODE** pPre,
NODE** pCur, char* target);
void printList (NODE* pList);
NODE* buildList (char* fileID);
int getData (FILE* fpData, STU* pData);
// Header file to include functions (Not in text)
int main (void)
{
// Local Declarations
NODE* pList;
NODE* pPre;
NODE* pCur;
STU data;
// Statements
printf("Begin list test driver\n\n");
// Build List
pList = buildList(GPA_DS);
if (!pList)
{
printf("Error building chron file\a\n");
exit (100);
} // if
printList (pList);
printf("\nTests complete.\n");
system("pause");
return 0;
} // main
/* ==================== insertNode ====================
This function inserts a single node into a linear list.
Pre pList is pointer to the list; may be null
pPre points to new node’s predecessor
item contains data to be inserted
Post returns the head pointer
*/
NODE* insertNode (NODE* pList, NODE* pPre, STU item)
{
// Local Declarations
NODE* pNew;
// Statements
if (!(pNew = (NODE*)malloc(sizeof(NODE))))
printf("\aMemory overflow in insert\n"), exit (100);
pNew->data = item;
if (pPre == NULL)
{
// Inserting before first node or to empty list
pNew->link = pList;
pList = pNew;
} // if pPre
else
{
// Inserting in middle or at end
pNew->link = pPre->link;
pPre->link = pNew;
} // else
return pList;
} // insertNode
/* ==================== searchList ====================
Given key value, finds the location of a node
Pre pList points to a head node
pPre points to variable to receive pred
pCur points to variable for current node
target is key being sought
Post pCur points to first node with >= key
-or- null if target > key of last node
pPre points to largest node < than key
-or- null if target < key of first node
function returns true if found
false if not found
*/
int searchList (NODE* pList, NODE** pPre,
NODE** pCur, char* target)
{
// Local Declarations
int found = 0;
// Statements
*pPre = NULL;
*pCur = pList;
// start the search from beginning
while (*pCur != NULL && (strcmp(target, (*pCur)->data.pin) > 0))
{
*pPre = *pCur;
*pCur = (*pCur)->link;
} // while
if (*pCur && (strcmp(target, (*pCur)->data.pin) == 0))
found = 1;
return found;
} // searchList
/* Traverse and print a linear list.
Pre pList is a valid linear list
Post List has been printed
*/
void printList (NODE* pList)
{
// Local Declarations
NODE* pWalker;
// Statements
pWalker = pList;
printf("List contains:\n");
while (pWalker)
{
printf("%s %s %4.2lf\n", pWalker->data.pin, pWalker->data.name, pWalker->data.gpa);
pWalker = pWalker->link;
} // while
printf("\n");
return;
} // printList
/* ==================== buildList ====================
This program builds a key-sequenced linear list.
Pre fileID is file that contains data for list
Post list built
returns pointer to head of list
*/
NODE* buildList (char* fileID)
{
// Local Declarations
STU data;
NODE* pList;
NODE* pPre;
NODE* pCur;
FILE* fpData;
// Statements
pList = NULL;
fpData = fopen(fileID, "r");
if (!fpData)
{
printf("Error opening file %s\a\n", fileID);
system("pause");
//exit (210);
} // if open fail
while (getData (fpData, &data))
{
// Determine insert position
searchList (pList, &pPre, &pCur, data.pin);
pList = insertNode(pList, pPre, data);
} // while
return pList;
} // buildList
/* ==================== getData ====================
Reads data from file.
Pre fpData is an open file
pData is pointer to input structure
Post data read
returns success/failure
*/
int getData (FILE* fpData, STU* pData)
{
// Local Definitions
int ioResult;
char temp[MAX_SIZE];
char *pGpa, *pEnd, *pName;
// Statements
fgets(temp, sizeof(temp), fpData);
sscanf(temp, "%4s", pData->pin);
pGpa = strrchr(temp, ';') + 1;
pData->gpa = strtod(pGpa, &pEnd);
*pGpa = '\0';
pName = temp + sizeof(pData->pin);
pData->name = (char *)calloc (strlen(pName) + 1, sizeof(char));
if(!pData->name)
printf("Error! Out of memory\n");
strcpy(pData->name, pName);
printf("%s %s %.2lf\n", pData->pin, pData->name, pData->gpa);
ioResult = 1;
//system("pause");
if (ioResult == 1)
return 1;
else
return 0;
} // getData
I'm getting really frustrated.. I know there's something in the buildList function but I can't seem to find the bug..