I'm having trouble dealing with the multiple lines of data in my text file:
Fred Flintstone 38 Male
Barney Rubble 36 Male
Wilma Flintstone 37 Female
Betty Rubble 36 Female
Pebbles Flintstone 4 Female
Bam-Bam Rubble 3 Male
Dino Flintstone 2 Male
Mr. Slate 57 Male
Hoppy Rubble 1 Male
Great Gazoo 11 Unknown
Note: There are tabs, not spaces, before and after the numbers.
I actually tried reading the data two ways, but both have been unsuccessful obviously. The first, my preferred method, does not involve an array of structures while the second one does. I'm thinking that using an array of structures maybe defeats the purpose of using linked lists. Just a guess. Still, I tried the second method anyway. I get no errors or warnings when I compile. I am using codeblocks as my compiler. Any tips on improving either one of my codes would be appreciated. Thank you.
Method one (preferred):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define LENGTH_OF_INPUT 35
typedef int KEY_TYPE;
typedef struct
{
char* name;
KEY_TYPE age;
char* gender;
}DATA;
typedef struct nodeTag
{
DATA character;
struct nodeTag* link;
}NODE;
NODE* buildList (NODE* pList);
bool getData (FILE* pData, DATA character);
NODE* insertNode (NODE*pList, NODE* pPre, DATA character);
void printList (NODE* pList);
int main(void)
{
NODE* pList;
pList = NULL;
pList = buildList (pList);
printList (pList);
return 0;
}
NODE* buildList (NODE* pList)
{
FILE* pData;
NODE* pPre;
DATA character;
pPre = NULL;
if ((pData = fopen("lab6data.txt", "r"))==NULL)
{
printf("Error opening text file.\n");
exit (100);
}
while (getData(pData, character));
{
pList = insertNode (pList, pPre, character);
}
if (fclose(pData) == EOF )
{
printf("Error closing lab6data.txt\n");
exit (300);
}
return pList;
}
bool getData (FILE* pData, DATA character)
{
char input [LENGTH_OF_INPUT];
char name [LENGTH_OF_INPUT];
char gender [LENGTH_OF_INPUT];
if (!feof(pData))
{
while (fgets(input, 34, pData))
{
sscanf(input, "%19[^\t]%d %[^\n]", name, &character.age, gender);
character.name = calloc(strlen(name) + 1, sizeof(char));
strcpy(character.name, name);
character.gender = calloc(strlen(gender) + 1, sizeof(char));
strcpy(character.gender, gender);
}
return true;
}
else
{
return false;
}
}
NODE* insertNode (NODE*pList, NODE* pPre, DATA character)
{
NODE* pNew;
if (!(pNew = (NODE*)malloc(sizeof(NODE))))
{
printf("Memory overflow in insert\n");
exit(200);
}
pNew->character = character;
pNew->link = NULL;
if (pPre == NULL)
{
pNew->link = NULL;
if (pList == NULL)
{
pList = pPre = pNew;
}
else
{
pPre->link = pNew;
pPre = pNew;
}
}
return pList;
}
void printList (NODE* pList)
{
NODE* pWalker;
pWalker = pList;
printf ("\nList contains:\n");
while (pWalker)
{
printf ("%s %d %s\n", pWalker->character.name, pWalker->character.age, pWalker->character.gender);
pWalker = pWalker->link;
}
printf ("<End of List>\n\n");
return;
}
Method 2:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH_0F_ARRAY 10
#define LENGTH_OF_INPUT 35
typedef int KEY_TYPE;
typedef struct
{
char* name;
KEY_TYPE age;
char* gender;
}DATA;
typedef struct nodeTag
{
DATA character;
struct nodeTag* link;
}NODE;
NODE* buildList (NODE* pList);
void getData (DATA* pChar);
NODE* insertNodes(NODE* pList, NODE* pPre, NODE* pCur, DATA* pChar);
void printList (NODE* pList);
int main(void)
{
NODE* pList;
pList = NULL;
pList = buildList (pList);
printList (pList);
return 0;
}
NODE* buildList (NODE* pList)
{
DATA* pChar;
NODE* pPre;
NODE* pCur;
pPre = NULL;
pCur = NULL;
pChar = calloc(LENGTH_0F_ARRAY, sizeof(DATA));
getData(pChar);
pList = insertNodes(pList, pPre, pCur, pChar);
return pList;
}
void getData (DATA* pChar)
{
FILE* pData;
int charIndex;
char input [LENGTH_OF_INPUT];
char name [LENGTH_OF_INPUT];
char gender [LENGTH_OF_INPUT];
if ((pData = fopen("lab6data.txt", "r"))==NULL)
{
printf("Error opening text file.\n");
exit (100);
}
charIndex = 0;
while (charIndex<LENGTH_0F_ARRAY && fgets(input, 34, pData))
{
sscanf(input, "%19[^\t]%d %[^\n]", name, &pChar[charIndex].age, gender);
pChar[charIndex].name = calloc(strlen(name) + 1, sizeof(char));
strcpy(pChar[charIndex].name, name);
pChar[charIndex].gender = calloc(strlen(gender) + 1, sizeof(char));
strcpy(pChar[charIndex].gender, gender);
charIndex++;
}
if (fclose(pData) == EOF )
{
printf("Error closing lab6data.txt\n");
exit (200);
}
return;
}
NODE* insertNodes(NODE* pList, NODE* pPre, NODE* pCur, DATA* pChar)
{
int charIndex;
NODE* pNew;
if (!(pNew = (NODE*)malloc(sizeof(NODE))))
{
printf("Memory overflow in insert\n"),
exit(300);
}
for (charIndex = 0; charIndex < LENGTH_0F_ARRAY; charIndex++)
{
pNew->character = pChar[charIndex];
pNew->link = NULL;
if (pList == NULL)
{
pList = pPre = pNew;
}
else
{
pPre->link = pNew;
pPre = pNew;
}
}
return pList;
}
void printList (NODE* pList)
{
NODE* pWalker;
pWalker = pList;
printf ("\nList contains:\n");
while (pWalker)
{
printf ("%s %d %s\n", pWalker->character.name, pWalker->character.age, pWalker->character.gender);
pWalker = pWalker->link;
}
printf ("<End of List>\n\n");
return;
}