hi, i have a flat file with names and item they pruchased (delimited by either a /,\,{,or}) which look like this:
john smith/soap
john brown/toilet paper
john smith/toilet paper
bruce smith/paper towel
etc etc
i need to read this txt file (m.txt) and put it into a sorted link list with the struct property(assuming that none of the names exceed 100 in length):
struct customer
{
struct customer *nextCustomer;
char name[100];
int appear;
};
typedef struct customer custNode;
the appear property is used if the customer name comes out more than once, then the appear should be incremented. note that the item the person purchased is not stored (this is done in another sub-system)
im able to read the file, but i cant split it into delimited parts and sort the linked list, nor can i increment the appear for the corresponding customer. here is what my code looks like:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SLENGTH 100
struct customer
{
struct customer *nextNode;
char name[SLENGTH];
int appear;
};
typedef struct customer custNode;
FILE *fin;
void exitIfFault(FILE *fp,char file[]);
FILE* fileOpen(char *file, char *mode);
void countC(char file[]);
custNode* createNode(char *custData);
int main(int args, char *arg[])
{
if (args != 2)
{
printf("\nSyntax : count filename\n");
return 1;
}
countC(arg[1]);
return 0;
}
void countC(char file[])
{
char tempo;
FILE *fp = fileOpen(file, "r");
custNode *startList=NULL;
custNode *tempPtr, *tailPtr = startList;
custNode aWord;
while (!feof(fp))
{
fscanf(fp, "%s", &tempo);
tempPtr = createNode(&tempo);
if (tailPtr == NULL)
{
startList = tailPtr = tempPtr;
}
else
{
if (exists(tempPtr->name,star
tList))
{
while (startList != NULL)
{
if (strcmp(tempPtr->name,star
tList->nam
e)==0)
{
startList->appear++;
}
startList = startList->nextNode;
}
}
else
{
tailPtr->nextNode = tempPtr;
tailPtr = tempPtr;
}
}
}
fclose(fp);
}
custNode* createNode(char *custData)
{
/*create new node for linked list. check malloc worked correctly and ensure node pointer set to NULL
*/
custNode *tempPtr;
tempPtr = (custNode*) malloc(sizeof(custNode));
if(tempPtr == NULL)
{
printf("Memory allocation error\n");
exit(EXIT_FAILURE);
}
strcpy(tempPtr->name,custD
ata);
tempPtr->nextNode = NULL;
return tempPtr;
}
void exitIfFault(FILE *fp,char file[])
{
char temp;
while (!feof(fp))
{
fscanf(fp, "%c", &temp);
if (temp != '\n')
{
printf("Error reading from %s\n",file);
fclose(fp);
exit(1);
}
}
}
FILE* fileOpen(char *file, char *mode)
{
FILE *fp = fopen(file,mode);
if ( fp == NULL)
{
printf("\nError - Unable to access %s\n",file);
exit(1);
}
return fp;
}
Note that the link list is stored in memory and will be used in other sub-systems.
thanks for your help