#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.
输入文件aka California.txt是一个简单的记事本文件,其中包含以下信息。
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.
在保存功能之后,所有数字都会通过,但以下是控制台打印出来的内容以及在California.txt文件中重写的内容。
{°@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.
我试图将char tempName(在load中找到的字符串)变成指针并运行这样的函数,然后控制台打印出并保存到加利福尼亚文件。
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
3 个解决方案
#1
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.
但是在load()中,您不为每个节点分配内存。你只需将它指向tempName,这是一个局部变量,所以在函数的末尾你会得到一个悬空指针。您需要为每个itemName分配内存,然后使用strcpy等函数将结果复制到其中。
Another option is to use a fix-length array instead, e.g.
另一种选择是使用固定长度数组,例如,
char itemName[80];
then strncpy
into it.
然后strncpy进入它。
#2
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.
将整个字符串从一个地方复制到另一个地方。在许多其他编程语言中,情况可能如此;在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.
所有这些语句都将缓冲区tempName的内存地址复制到指针变量tempNode-> itemName中。这在两个方面是错误的。
- 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.
- 通过反复执行此操作,链接列表中的所有节点最终都会引用相同的字符串缓冲区。重复覆盖此缓冲区;最后,剩下的就是从文件中读取的最后一行。无论您访问哪个节点,您都将看到相同的字符串。
-
tempName
is an automatic variable. Its memory is likely to be reclaimed as soon as the program flow leaves functionload
. The linked list keeps on referring to that memory afterwards. This is a major bug that leads to undefined behavior. - tempName是一个自动变量。一旦程序流程离开功能负载,它的内存很可能会被回收。链表继续引用该存储器。这是一个导致未定义行为的主要错误。
The solution is to duplicate the string. This is easy to do with function strdup
. Just replace this line of code:
解决方案是复制字符串。使用函数strdup很容易做到这一点。只需替换这行代码:
tempNode->itemName = tempName;
by:
通过:
tempNode->itemName = strdup(tempName);
#3
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
tempNode-> itemName = tempName; <=这里你总是得到相同的指针,而不是值
Try use: strcpy(tempNode->itemName, tempName);
and see if it works. ;)
尝试使用:strcpy(tempNode-> itemName,tempName);并看看它是否有效。 ;)
#1
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.
但是在load()中,您不为每个节点分配内存。你只需将它指向tempName,这是一个局部变量,所以在函数的末尾你会得到一个悬空指针。您需要为每个itemName分配内存,然后使用strcpy等函数将结果复制到其中。
Another option is to use a fix-length array instead, e.g.
另一种选择是使用固定长度数组,例如,
char itemName[80];
then strncpy
into it.
然后strncpy进入它。
#2
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.
将整个字符串从一个地方复制到另一个地方。在许多其他编程语言中,情况可能如此;在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.
所有这些语句都将缓冲区tempName的内存地址复制到指针变量tempNode-> itemName中。这在两个方面是错误的。
- 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.
- 通过反复执行此操作,链接列表中的所有节点最终都会引用相同的字符串缓冲区。重复覆盖此缓冲区;最后,剩下的就是从文件中读取的最后一行。无论您访问哪个节点,您都将看到相同的字符串。
-
tempName
is an automatic variable. Its memory is likely to be reclaimed as soon as the program flow leaves functionload
. The linked list keeps on referring to that memory afterwards. This is a major bug that leads to undefined behavior. - tempName是一个自动变量。一旦程序流程离开功能负载,它的内存很可能会被回收。链表继续引用该存储器。这是一个导致未定义行为的主要错误。
The solution is to duplicate the string. This is easy to do with function strdup
. Just replace this line of code:
解决方案是复制字符串。使用函数strdup很容易做到这一点。只需替换这行代码:
tempNode->itemName = tempName;
by:
通过:
tempNode->itemName = strdup(tempName);
#3
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
tempNode-> itemName = tempName; <=这里你总是得到相同的指针,而不是值
Try use: strcpy(tempNode->itemName, tempName);
and see if it works. ;)
尝试使用:strcpy(tempNode-> itemName,tempName);并看看它是否有效。 ;)