我是初学者。当按下加载按钮时,我制作了一个程序来加载和保存二进制文件中的数据。但是文件中显示的数据只是垃圾数据。
#include <stdio.h>
#define size 5
#define enter 13
#define esc 27
#define home 71
#define end 79
#define up 72
#define down 80
struct node {
int id;
char name[30];
struct node* prev;
struct node* next;
};
struct node* head = NULL;
struct node* tail = NULL;
void save_to_file();
void display_list();
void append_node(struct node* temp);
struct node* create_node_from_file(int id, char name[]);
struct node* create_node();
int check_for_id(int id);
struct node* search_by_id(int s);
load_from_file() ;
void main ()
{
char ch;
int flag = 0, i, num;
int pos = 0;
char word[5][30] = {"add", "Display", "Save", "Load", "Exit"};
struct node* t;
int id;
char searchname[30];
clrscr();
do
{
clrscr();
for(i = 0; i < 5; i++)
{
gotoxy(10, 5 + i);
if(i == pos)
{
textattr(0x70);
}
cprintf("%s", word[i]);
textattr(0x07);
}
// textattr(0x06);
ch = getch();
switch(ch)
{
case enter:
clrscr();
switch(pos)
{
case 0:
t = create_node();
append_node(t);
printf("\n\t\tInsertion succeessful !");
break;
case 1:
display_list();
break;
case 2:
save_to_file();
break;
case 3:
load_from_file();
break;
case 4:
printf("exit");
flag = 1;
break;
}
case NULL:
ch=getch();
switch(ch)
{
case home:
pos = 0;
break;
case end:
pos = 4;
break;
case up:
pos--;
if(pos < 0)
pos = 4;
break;
case down:
pos++;
if(pos > 4)
pos = 0;
break;
}
break;
}
} while(flag !=1);
getch();
}
struct node* create_node() {
struct node* temp;
int tryid;
int t;
int flag = 0;
temp = (struct node*)malloc(sizeof(struct node));
while(flag != 1) {
printf("\n\tPlease Enter Id: ");
scanf("%d", &tryid);
t = check_for_id(tryid);
if(t == 0) {
temp->id = tryid;
flag = 1;
}
else
printf("\nId %d already exists!", tryid);
}
printf("\n\tPlease Enter Name: ");
flushall();
gets(temp->name);
temp->prev = NULL;
temp->next = NULL;
return temp;
}
int check_for_id(int id) {
struct node* ptr;
ptr = search_by_id(id);
if(ptr != NULL)
return 1;
return 0;
}
struct node* search_by_id(int s) {
struct node* ptr;
ptr = head;
while(ptr != NULL) {
if(ptr->id == s)
return ptr;
ptr = ptr->next;
}
return NULL;
}
struct node* create_node_from_file(int id, char name[]) {
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->id = id;
strcpy(temp->name, name);
temp->prev = NULL;
temp->next = NULL;
return temp;
}
void append_node(struct node* temp) {
if(head == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void display_list() {
struct node* temp;
//sort();
temp = head;
if(head == NULL && tail == NULL) {
printf("\n\tList is Empty!\n\t\tNo Data..");
return;
}
while(temp != NULL) {
printf("\n%d", temp->id);
printf("\t\t\t%s\n", temp->name);
temp = temp->next;
}
return;
}
void save_to_file(struct node emp) {
int id;
int index=0;
char name[30];
struct node* ptr;
FILE* fp;
//sort();
ptr = head;
fp = fopen("file.bin", "wb"); //open file for writing
while(ptr != NULL) {
fwrite(ptr,sizeof(*ptr), 2, fp);
ptr = ptr->next;
}
fclose(fp);
gotoxy(30,10);
printf("Data saved to file.");
return;
}
load_from_file()
{
struct node emp;
FILE *fptr;
fptr = fopen("file.bin", "rb");
while(feof(fptr) == NULL)
{
fread(&emp, sizeof(emp), 1, fptr);
printf("%d %s", emp.id, emp.name);
printf("\n");
}
fclose(fptr);
// rewind(fptr);
}
代码有什么问题?我该如何改变呢?