我在尝试将程序中的信息保存到.txt文件(或任何其他文件)时遇到了一些问题。我已经多次查看了我的代码,但似乎找不到问题所在。最初,我认为可能存在某种内存泄漏(尽管我不知道内存泄漏的后果,所以不能确定)。
我想澄清这是一个学校作业,这是我们最后也是最大的任务。我们正在使用结构体创建一个购物清单。问题出现在我试图使用结构体成员将信息保存到.txt文件中(如果需要,稍后可以将其加载到程序中)。我知道代码可能很糟糕,很难看,但请耐心等待。
这是我的“保存”函数。它非常基础而且相当糟糕。
这是我在程序中输入的内容(添加项目时):
这是保存在我的.txt文件中的内容(它没有显示,但第一行总是包含一些奇怪的符号)。
同样的问题一直存在;第一个项目名称(例如鳄梨)显示为一些奇怪的符号。
这是我的完整代码,问题可能出现在这里某个地方。
如果我的代码在某些地方看起来很奇怪(为什么你有一个void inputError()?),那是因为我们老师对我们的作业设置了一些非常奇怪的规则。请随意抨击我的代码。
我想澄清这是一个学校作业,这是我们最后也是最大的任务。我们正在使用结构体创建一个购物清单。问题出现在我试图使用结构体成员将信息保存到.txt文件中(如果需要,稍后可以将其加载到程序中)。我知道代码可能很糟糕,很难看,但请耐心等待。
这是我的“保存”函数。它非常基础而且相当糟糕。
void saveList(struct GList *grocery)
{
char file[20];
FILE *fp;
printf("What do you want to save the list as? (Don't include file extension): ");
scanf("%s", file);
fp = fopen(strcat(file, ".txt"), "w");
for (int i=0; i<grocery->Items; i++)
{
printf("%s %f %s\n", grocery->list[i].name, grocery->list[i].amount, grocery->list[i].unit);
fprintf(fp, "%s %f %s\n", grocery->list[i].name, grocery->list[i].amount, grocery->list[i].unit);
}
fclose(fp);
}
这是我在程序中输入的内容(添加项目时):
Name of the item: Avocado
Unit: kg
Amount: 10
这是保存在我的.txt文件中的内容(它没有显示,但第一行总是包含一些奇怪的符号)。
10.000000 kg
milk 10.000000 litres
同样的问题一直存在;第一个项目名称(例如鳄梨)显示为一些奇怪的符号。
这是我的完整代码,问题可能出现在这里某个地方。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
struct Grocery {
char name[20];
char unit[20];
float amount;
};
struct GList {
size_t Items;
struct Grocery *list;
};
int addGrocery();
void printList();
void hQuit();
void inputError();
void removeItem();
void changeItem();
void saveList();
int main()
{
struct GList glist;
glist.Items = 1;
size_t menuChoice = 0;
char cont = 'y';
if((glist.list = malloc(sizeof(glist.list))) == NULL)
return ENOMEM;
puts("Welcome to your Grocery List Manager");
do
{
printf("\n- - - - - - - - - - -\n[1] Add an item\n[2] Print grocery list\n[3] Remove a grocery\n[4] Edit a grocery\n[5] Save your list\n[6] Load a list\n[7] Quit\n\nPlease choose action: ");
if(!scanf("%u", &menuChoice))
return EIO;
putchar('\n');
switch(menuChoice)
{
case 1:
addGrocery(&glist);
break;
case 2:
printList(&glist);
break;
case 3:
removeItem(&glist);
break;
case 4:
changeItem(&glist);
break;
case 5:
saveList(&glist);
break;
case 6:
//Load shopping list
break;
case 7:
hQuit(&glist);
break;
default:
inputError();
break;
}
} while (cont == 'y');
//free(grocery);
return 0;
}
int addGrocery(struct GList *grocery)
{
printf("Name of the grocery: ");
if(!scanf("%s", grocery->list[grocery->Items].name))
return EIO;
printf("Unit: ");
if(!scanf("%s", grocery->list[grocery->Items].unit))
return EIO;
printf("Amount: ");
if(!scanf("%f", &grocery->list[grocery->Items].amount))
return EIO;
printf("You have added %f %s of %s into your list!\n\n", grocery->list[grocery->Items].amount, grocery->list[grocery->Items].unit, grocery->list[grocery->Items].name);
(grocery->Items)++;
grocery->list = realloc(grocery->list, grocery->Items * sizeof(grocery->list));
if(grocery->list == NULL)
return ENOMEM;
return 1;
}
void printList(struct GList *grocery)
{
if ((grocery->Items - 1) > 0)
printf("You have added %d item(s) into your list!\n", grocery->Items - 1);
else
printf("You have no items in your list!\n");
for (int i=1; i<grocery->Items; i++)
{
printf("[%d] %-10s %.1f %s\n", i, grocery->list[i].name, grocery->list[i].amount, grocery->list[i].unit);
}
putchar('\n');
}
void removeItem(struct GList *grocery)
{
size_t index = 0;
printf("Which item would you wish to remove from the list? ");
scanf("%u", &index);
printf("\nYou have removed %s from your grocery list!", grocery->list[index].name);
for (int i=(int)index; i < grocery->Items; i++)
grocery->list[i] = grocery->list[i+1];
(grocery->Items)--;
}
void changeItem(struct GList *grocery)
{
size_t index = 0;
printf("Which item would you like to edit the amount of? ");
scanf("%d", &index);
printf("\nCurrent amount: %.1f %s\nEnter new amount: ", grocery->list[index].amount, grocery->list[index].unit);
scanf("%f", &grocery->list[index].amount);
printf("\nYou changed the amount to %.1f!\n", grocery->list[index].amount);
}
void hQuit(struct GList *grocery)
{
puts("*-*-* Thank you for using the Grocery List! *-*-*");
free(grocery->list);
exit(0);
}
void inputError(struct GList *grocery)
{
puts("No such option. Please try again!\n");
}
void saveList(struct GList *grocery)
{
char file[20];
FILE *fp;
printf("What do you want to save the list as? (Don't include file extension): ");
scanf("%s", file);
fp = fopen(strcat(file, ".txt"), "w");
for (int i=0; i<grocery->Items; i++)
{
printf("%s %f %s\n", grocery->list[i].name, grocery->list[i].amount, grocery->list[i].unit);
fprintf(fp, "%s %f %s\n", grocery->list[i].name, grocery->list[i].amount, grocery->list[i].unit);
}
fclose(fp);
}
如果我的代码在某些地方看起来很奇怪(为什么你有一个void inputError()?),那是因为我们老师对我们的作业设置了一些非常奇怪的规则。请随意抨击我的代码。
T*p=[m/c/re]alloc(sizeof T*)
是错误的。 - EOFn
个指针。现在它们指向什么? - Lee Daniel Crocker