我的編程出現錯誤 在case'4' 和 case'5' 這兩個地方
case'4'是儲存檔案在資料夾 case'5'是讀取檔案從資料夾
不知道哪位高手幫幫忙
謝謝
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"
int main(int argc, char *argv[])
{
int i;
struct Info info;
char selection;
FILE *fptr;
int size;
printf("Welcome to the storage room.\n\n");
do
{
printf("Please select an option that you wish.");
printf("\n*************************************\n");
printf("Press 1 to add a record.\n");
printf("Press 2 to delete a record.\n");
printf("Press 3 to view all records.\n");
printf("Press 4 to saving records to a disk file.\n");
printf("Press 5 to restoring records from a disk file.\n");
printf("Press 'ESC' to exit.");
printf("\n*************************************\n");
printf("\n");
selection = getch();
switch( selection )
{
case '1':
if (isEmptyList())
{
printf("The list is empty.\n");
}
for (i=0; i<50; i++)
{
printf("record number: ");
scanf(" %d", &info.record);
printf("Name of compenent type: ");
fflush(stdin);
gets(info.type);
printf("The value of component: ");
fflush(stdin);
gets(info.value);
printf("How many in the stock: ");
scanf(" %d", &info.number);
insertNode(&info);
}
break;
case '2':
if (isEmptyList())
{
printf("The list is empty.\n");
}
while(traversalList());
deleteNode();
break;
case '3':
if (isEmptyList())
{
printf("The list is empty.\n");
break;
}
rewindList();
do
{
retrieveNode(&info);
printf("record number\t%d\n", info.record);
printf("compenent type:\t%s\n", info.type);
printf("component value:\t%s\n", info.value);
printf("stock:\t%3d\n", info.number);
} while (traversalList());
break;
case '4'
fptr = fopen("comp.dat","wb");//open the file for writing
while(!isEmptyList())
{
rewindList();
retrieveNode(&info);
fwrite(&info,sizeof(info),1, fptr);
deleteNode();
}
fclose(fptr);
break;
case '5'
fptr = fopen("MyData.dat","rb"); //open the file for
reading
//read from the file
while(1)
{
size = fread(&info,sizeof(info),1,fptr);
if (size <= 0)
{
break;
}
insertNode(&info);
fclose(fptr);
case 27:
printf("Bye.\n\n");
break;
default:
printf("Your selection is not valid.\n");
printf("Please try again\n");
printf("\n");
break;
}
}while (selection != 27);
system("PAUSE");
return 0;
}
以下是linkedlist.c 的部分(應該是沒錯)
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
//internal function prototypes
void insert();
void create();
void assign(struct Info *info);
struct Node *head=NULL, *current=NULL, *temp=NULL;
int isEmptyList()
{
return (head==NULL);
}
void rewindList()
{
current = head;
}
int traversalList()
{
if (current==NULL) return 0;
if (current->link==NULL) return 0;
current = current->link;
return 1;
}
void insert()
{
if (head == NULL)
{
head = temp;
current = head;
}
else
{
temp->link = current->link;
current->link = temp;
current = current->link;
}
}
void deleteNode()
{
if (head==NULL) return;
if (current==head)
{
head = current->link;
free(current);
current = head;
}
else
{
temp = head;
while(1)
{
if (temp->link==current) break;
temp = temp->link;
}
temp->link = current->link;
free(current);
current = temp->link;
if (current==NULL) current = head;
}
}
void create()
{
temp = (struct Node *)malloc(sizeof(struct Node));
temp->link = NULL;
}
void assign(struct Info *info)
{
if (temp==NULL) return;
memcpy(&temp->info,info,sizeof(struct Info));
}
void retrieveNode(struct Info *info)
{
if (head==NULL || current==NULL) return;
memcpy(info,¤t->info,sizeof(struct Info));
}
void insertNode(struct Info *info)
{
create();
assign(info);
insert();
}
以下是linkedlist.h 的部分(應該是沒錯)
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
//declare struct for data
struct Info
{
int number;
int record;
char type[25];
char value[25];
};
struct Node
{
struct Info info;
struct Node *link;
};
//function prototypes
int isEmptyList();
void rewindList();
int traversalList();
void insertNode(struct Info *info);
void deleteNode();
void retrieveNode(struct Info *info);
#endif