gpt4 book ai didi

c - 如何修复 "Access violation writing location 0xFFFFFFCD."?

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:32 25 4
gpt4 key购买 nike

我想从文件中读取数据并将其保存到链表中,我的问题显然出在读取命令“fscanf”上。

我正在尝试创建一个函数来接收链表的头部和指向文件的指针。该函数从文件中读取数据并将它们保存到一个节点中,然后将该节点连接到链表的开头,即到头,而不是到末尾。

#include <stdio.h>
#include <stdlib.h>

#define NameLength 15

typedef struct Product {
char ProductName[NameLength];
int Quantity;
int Price;
char Premium;
}Product;

typedef struct ProductList {
Product P;
struct ProductList* next;
}ProductList;

void DeleteList(ProductList*);
void ErrorMsg(const char*);
int CheckInList(ProductList*, const char*);

void CreateProducts(ProductList *head, FILE *fp) {
ProductList *temp = (ProductList*)malloc(sizeof(ProductList));
//If the dynamic memory allocation for temp has failed, print a
message and exit the program.
if (!temp) {
DeleteList(head);
ErrorMsg("Error: Memory allocation of temp in CreateProducts has
failed.");
}

temp->next = NULL;
while (!feof(fp)) {
fscanf(fp, "%s%d%d%c", temp->P.ProductName, &temp->P.Quantity,
&temp->P.Price, temp->P.Premium);
if (CheckInList(head, temp->P.ProductName))
printf("Error: Product is already found in the list.\n");
else {
if (temp->P.Quantity < 0)
printf("Error: Quantity of the product cannot be
negative.\n");
else {
if (temp->P.Price < 0)
printf("Error: Price of the product cannot be
negative.\n");
else
{
//Adding the product to the beginning of the list
every time.
if (head == NULL)
head = temp;
else
{
temp->next = head->next;
head->next = temp;
}
}
}
}
}
if (head != NULL)
printf("Products' information have been received.\n");
else
ErrorMsg("Products' information have NOT been received.");
}

最佳答案

打开编译器的警告! It will literally give you the answer.

main.cpp: In function 'void CreateProducts(ProductList*, FILE*)':
main.cpp:33:20: warning: format '%c' expects argument of type 'char*', but argument 6 has type 'int' [-Wformat=]
fscanf(fp, "%s%d%d%c", temp->P.ProductName, &temp->P.Quantity,
^~~~~~~~~~
&temp->P.Price, temp->P.Premium);
~~~~~~~~~~~~~~~

(我知道它说 main.cpp;那只是在线编译器的产物,我将其置于 C 模式。)

关于c - 如何修复 "Access violation writing location 0xFFFFFFCD."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56633358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com