从二进制文件(C)读取结构
我需要你的帮助从二进制文件中读取结构, “从文件()读取文件()…”函数无法使其工作 这是我的密码:从二进制文件(C)读取结构,c,struct,binary,C,Struct,Binary,我需要你的帮助从二进制文件中读取结构, “从文件()读取文件()…”函数无法使其工作 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <ctype.h> #include <conio.h> struct grocerylist { int iD; char grocery[30]; float amoun
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>
struct grocerylist
{
int iD;
char grocery[30];
float amount;
char unit[10];
};
Grocery set_size_of(Grocery* list, int index);
Grocery input(int index);
void print_grocerylist(Grocery* list, int* index);
void write_to_file(FILE* file_pointer, Grocery* list, int index);
Grocery read_from_file(FILE* file_pointer, Grocery* list, int* index);
void change_item(Grocery* list, int index);
void remove_item(Grocery* list, int* index);
int main()
{
Grocery* list = (Grocery*)malloc(sizeof(Grocery)*1); //Grocery-array allocated memory for 1 struct
int index = 0, choice = 0;
FILE* file_pointer = NULL;
printf("Hey there user!\n");
while(choice != 7){
system("cls");
printf("Menu\n");
printf("1 - Add an item to the list.\n2 - Print the contents of your current list.\n3 - Print the contents to file.\n4 - Read from file.\n5 - Change an item on the list.\n6 - Remove item from the list.\n7 - close the program.\nChoose: ");
scanf("%d", &choice);
printf("\n");
switch(choice){
case 1:
list[index] = input(index);
index++;
*list = set_size_of(list, index); //Reallocating memory for the array of grocery-structs.
fflush(stdin);
break; //Add item
case 2:
print_grocerylist(list, &index); //Print list
fflush(stdin);
break;
case 3:
write_to_file(file_pointer, list, index); //print to file
fflush(stdin);
break;
case 4:
*list = read_from_file(file_pointer, list, &index); //read from file
fflush(stdin);
break;
case 5:
change_item(list, index); //change item
fflush(stdin);
break;
case 6:
remove_item(list, &index); //remove item
index--;
fflush(stdin);
break;
case 7:
free(list);
exit(EXIT_SUCCESS); //End program
break;
default:
printf("Wrong input, please try again!");
fflush(stdin);
getch();
break;
}
}
return 0;
}
Grocery set_size_of(Grocery* list, int index){ //reallocates memory.
list = realloc(list, (index+1)*sizeof(Grocery));
return *list;
}
Grocery input(index){
Grocery tmp;
int x = 0;
tmp.iD = index +1;
fflush(stdin);
printf("Grocery: ");
gets(tmp.grocery);
while(x == 0){
fflush(stdin);
printf("Amount: ");
x = scanf("%f", &tmp.amount);
if(x == 0){
puts("Error, please try again!");
}
}
fflush(stdin);
x = 0;
while(x == 0){
printf("Unit: ");
gets(tmp.unit);
if(isalpha(*tmp.unit)){
x = 1;
}else{
x = 0;
puts("Error: please try again!\n");
}
}
printf("\n");
return tmp;
}
void print_grocerylist(Grocery* list, int* index){
int i;
system("cls");
printf("The grocery list contains:\n");
printf("Item Amount Unit");
for(i=0;i<*index;i++)
{
printf("\n%d: %-10s %5.1f %6s", list[i].iD, list[i].grocery, list[i].amount, list[i].unit);
}
printf("\n");
getch();
printf("Press any key to continue.\n");
}
void write_to_file(FILE* file_pointer, Grocery* list, int index){ //works just fine afaik.. Prints the index at the start, fills it up with all the rest.
char filename[30];
memset(filename, '\0', sizeof(filename));
printf("What is the name of the file you wish to write to?: ");
scanf("%s", filename);
strcat(filename, ".txt");
file_pointer = fopen(filename, "wb");
if(file_pointer == NULL){
printf("Error!");
return;
}else{
fflush(stdin);
printf("File opened successfully!\n");
fwrite(&index, sizeof(int), 1, file_pointer);
fwrite(list, sizeof(Grocery), index, file_pointer);
printf("File written.");
getch();
}
fclose(file_pointer);
return;
}
Grocery read_from_file(FILE* file_pointer, Grocery* list, int* index){ //Can't get this to work...
char filename[30];
memset(filename, '\0', sizeof(filename));
printf("What is the name of the file you wish to read from?: ");
scanf("%s", filename);
strcat(filename, ".txt");
file_pointer = fopen(filename, "rb");
if(file_pointer == NULL){
printf("Error!");
return *list;
}else{
fflush(stdin);
free(list);
printf("File opened successfully!\n");
fread(index, sizeof(int), 1, file_pointer);
printf("Index %d read.\n", *index);
list = (Grocery*)calloc(*index, sizeof(Grocery));
fread(list, sizeof(Grocery), (*index), file_pointer);
printf("File read.");
getch();
fflush(stdin);
}
fclose(file_pointer);
return *list;
}
void change_item(Grocery* list, int index){ //Basically it's a more complex version of "Grocery input()" were we look to the ID/index and alter the information.
int x = 0, id, item, choice;
print_grocerylist(list, &index);
printf("Which item would you like to change?\nID number: ");
scanf("%d", &id);
printf("\nWhat do you want to change?\n1 - Grocery.\n2 - Amount.\n3 - Unit.\nChoose: ");
scanf("%d", &choice);
switch(choice){
case 1:
fflush(stdin);
printf("Grocery: ");
for(item = 0; item <= index; item++){
if(list[item].iD == id){
gets(list[item].grocery);
}
}
break;
case 2:
while(x == 0){
fflush(stdin);
printf("Amount: ");
for(item = 0; item <= index; item++){
if(list[item].iD == id){
x = scanf("%f", &list[item].amount);
}
}
if(x == 0){
puts("Error, please try again!");
}
}
break;
case 3:
fflush(stdin);
x = 0;
printf("Unit: ");
for(item = 0; item <= index; item++){
if(list[item].iD == id){
gets(list[item].unit);
if(isalpha(*list[item-1].unit)){
x = 1;
}else{
x = 0;
puts("Error: please try again!\n");
}
}
}
break;
default:
printf("\nError! Please try again.\n");
break;
}
return;
}
void remove_item(Grocery* list, int* index){ //Takes the last struct and copies it to the slot which will be "removed" and then reallocates the memory for the array -1, keeping the old ID, to make things simple.
int item;
printf("Which item would you like to remove?\nID number: ");
scanf("%d", &item);
strcpy(list[item].grocery, list[*index].grocery);
list[item].amount = list[*index].amount;
strcpy(list[item].unit, list[*index].unit);
list = realloc(list, (*index-1)*sizeof(Grocery));
return ;
}
#包括
#包括
#包括
#包括
#包括
结构杂货店
{
int-iD;
煤焦食品[30];
浮动金额;
字符单位[10];
};
杂货集大小(杂货*列表,整数索引);
食品输入(国际指数);
作废打印_杂货店列表(杂货店*列表,整数*索引);
无效写入文件(文件*文件\指针、杂货店*列表、整数索引);
从文件读取(文件*文件\指针,杂货*列表,整数*索引);
作废变更项目(杂货*清单,内部索引);
作废删除项目(杂货*清单,整数*索引);
int main()
{
杂货店*列表=(杂货店*)malloc(sizeof(杂货店)*1);//杂货店数组为1个结构分配了内存
int index=0,choice=0;
FILE*FILE\u指针=NULL;
printf(“你好,用户!\n”);
while(选项!=7){
系统(“cls”);
printf(“菜单\n”);
printf(“1-向列表中添加项目。\n2-打印当前列表的内容。\n3-将内容打印到文件。\n4-从文件读取。\n5-更改列表中的项目。\n6-从列表中删除项目。\n7-关闭程序。\n选择:”;
scanf(“%d”,选择(&C);
printf(“\n”);
开关(选择){
案例1:
列表[索引]=输入(索引);
索引++;
*list=set_size_of(list,index);//为结构数组重新分配内存。
fflush(stdin);
break;//添加项
案例2:
print_grocerylist(列表和索引);//打印列表
fflush(stdin);
打破
案例3:
写入文件(文件指针、列表、索引);//打印到文件
fflush(stdin);
打破
案例4:
*list=read_from_file(文件指针、列表和索引);//read from file
fflush(stdin);
打破
案例5:
更改项目(列表、索引);//更改项目
fflush(stdin);
打破
案例6:
删除项目(列表和索引);//删除项目
索引--;
fflush(stdin);
打破
案例7:
免费(名单);
退出(退出成功);//结束程序
打破
违约:
printf(“输入错误,请重试!”);
fflush(stdin);
getch();
打破
}
}
返回0;
}
杂货集大小(杂货*列表,int索引){//重新分配内存。
list=realloc(列表,(索引+1)*sizeof(杂货店));
返回*列表;
}
食品输入(索引){
食品杂货tmp;
int x=0;
tmp.iD=指数+1;
fflush(stdin);
printf(“杂货店:”);
gets(tmp.gery);
而(x==0){
fflush(stdin);
printf(“金额:”);
x=scanf(“%f”和tmp.金额);
如果(x==0){
puts(“错误,请重试!”);
}
}
fflush(stdin);
x=0;
而(x==0){
printf(单位:);
获取(tmp.unit);
if(isalpha(*tmp.单位)){
x=1;
}否则{
x=0;
puts(“错误:请重试!\n”);
}
}
printf(“\n”);
返回tmp;
}
作废打印_杂货店列表(杂货店*列表,整数*索引){
int i;
系统(“cls”);
printf(“杂货清单包含:\n”);
printf(“项目金额单位”);
对于(i=0;i大多数情况下,“screery read_from_file()”函数可以工作,但它只返回第一项,而不是正确读取的列表
要返回并分配(指向)整个列表,请更改调用
*list = read_from_file(file_pointer, list, &index);
到
定义是什么
Grocery read_from_file(FILE* file_pointer, Grocery* list, int* index){
…
return *list;
…
return *list;
}
到
若要在从文件中读取旧的列表时不泄漏其内存,请在list=(杂货店*)calloc(*index,sizeof(杂货店));
之前插入free(list)
;或将其更改为
list = realloc(list, *index * sizeof(Grocery));
两个词:字节对齐(我所有其他的“词”都集中在你的问题的结构有多糟糕…。欢迎,请执行堆栈溢出教程“无法使其工作”经典的糟糕问题。什么不起作用?编译、崩溃的可执行文件或错误的输出?请突出您遇到问题的具体点。请原谅我的英语不好。问题是,当我从文件读取后要添加新对象时,程序崩溃。当您的程序“崩溃”时,会出现什么错误?
Grocery *read_from_file(FILE *file_pointer, Grocery *list, int *index)
{
…
return list;
…
return list;
}
list = realloc(list, *index * sizeof(Grocery));