#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct node
{
int priorityc;
char *itemName;
struct node *next;
} node;
node *head;
void save();
void printcontents();
int search(char *itemName);
int serve(char *itemName);
void load();
void inserted();
void deleted(char *itemName);
int main()
{
load();
printcontents();
save();
}
void load()
{
head = NULL;
FILE *fp;
fp = fopen("California.txt", "r");
int tempPriority;
char tempName[200];
while(fscanf(fp, "%s %d", tempName, &tempPriority) == 2)
{
//The issue seems to arise somewhere in the remaining code of the load function
node *tempNode = (node *)malloc(sizeof(struct node));
tempNode->priorityc = tempPriority;
tempNode->itemName = tempName;
tempNode->next = head;
head = tempNode;
printf("%s\n", head->itemName);
}
fclose(fp);
printf("%s\n", head->itemName);
}
void printcontents()
{
node *current = head;
while(current != NULL)
{
printf("%s %d\n", current->itemName, current->priorityc);
current=current->next;
}
}
void save()
{
FILE *fp;
node *current = head;
fp = fopen("California.txt", "w");
while(current != NULL)
{
fprintf(fp, "%s %d\n", current->itemName, current->priorityc);
current=current->next;
}
fclose(fp);
}
The input file aka California.txt is a simple notepad file with the following information.
Desktop-computer 100
Desktop-screen 100
Desktop-keyboard 100
TV-set 80
Audio-system 75
Bed 65
Night-table 65
Hibachi 35
After the save function all the numbers do make it through but the following is what the console prints out and what is rewritten on the California.txt file.
{°@u&0@ 35
{°@u&0@ 65
{°@u&0@ 65
{°@u&0@ 75
{°@u&0@ 80
{°@u&0@ 100
{°@u&0@ 100
{°@u&0@ 100
I tried to making char tempName (string found in load) into a pointer and running the function like that but then the console prints out and saves to california file.
Hibachi 35
Hibachi 65
Hibachi 65
Hibachi 75
Hibachi 80
Hibachi 100
Hibachi 100
Hibachi 100
I've really run into a dead end here and I can't figure out how to fix the problem, any advice would help. If it was working correctly it should print out the following to the console.
Hibachi 35
Night-table 65
Bed 65
Audio-system 75
TV-set 80
Desktop-keyboard 100
Desktop-screen 100
Desktop-computer 100