0
#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
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Mr. Lief
  • 1
  • 1

3 Answers3

1

Your node stores the names via the pointer

char *itemName;

However in load() you do not allocate memory for each node. You just point it to tempName, which is a local variable, so at the end of the function you end up with a dangling pointer. You need to allocate memory for each itemName, then use a function such as strcpy to copy the result into it.

Another option is to use a fix-length array instead, e.g.

char itemName[80];

then strncpy into it.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
1

You may be under the false impression that your assignment statement:

tempNode->itemName = tempName;

copies the entire string from one place to another. In many other programming languages this may be the case; not so in C.

All this statement does, is copy the memory address of buffer tempName into the pointer variable tempNode->itemName. This is wrong in two ways.

  1. By repeatedly doing this, eventually all nodes in your linked list have a reference to the same string buffer. This buffer is overwritten repeatedly; at the end, all that is left is the last line read from file. Whatever node you access, you will always see that same string.
  2. tempName is an automatic variable. Its memory is likely to be reclaimed as soon as the program flow leaves function load. The linked list keeps on referring to that memory afterwards. This is a major bug that leads to undefined behavior.

The solution is to duplicate the string. This is easy to do with function strdup. Just replace this line of code:

tempNode->itemName = tempName;

by:

tempNode->itemName = strdup(tempName);
Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45
0

I think your problem is that you are saving the same pointer.

Look:

typedef struct node
{
 int priorityc;
 char *itemName; <= here you have pointer
 struct node *next;
} node;`

And,

tempNode->itemName = tempName; <= and here you get always the same pointer, not the value

Try use: strcpy(tempNode->itemName, tempName); and see if it works. ;)

Rodrigo Rech
  • 11
  • 1
  • 3