|
Urgent ! Pls Help Me !
hi all,
I wanna your help urgently, bcs i wanna submit my project at 7/11/03 at that time. As a result, i am still unable to solve my code.
Got error ! that why ... he he .. wanna help !
My program is student database system.
Specification must included dynamic data struture and binary file.
Pls help me to check wht error for my code and if you hv time pls give me solution or source sample i can refer.
Problem Program
--------------------
1.Unable to read/write data into file.
2.Delete function will critical message when i delete data frm it.
Below is my sources code.
Compiler: VC++
Thks A LOT THKS A LOT !! HELP ME PLS !!
==============================================
// Header Files
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include "mainMenu.h"
#define TRUE 1
#define FALSE 0
//////////////////////////////////////////////////////////////////////////////////////////////
// Data structure defintiion
struct stud_record {
char name[25];
int id;
float height;
float weight;
};
struct stud_node {
struct stud_record data;
struct stud_node *next;
};
//////////////////////////////////////////////////////////////////////////////////////////////
// Function prototype declaration
int mainMenu();
int isEmpty(struct stud_node *start);
void record_list(struct stud_node *head);
void title(void);
void spc(char text[]);
struct stud_node * insert(struct stud_record temp, struct stud_node *start);
struct stud_node *insertFirst (struct stud_record *temp, struct stud_node *start);
struct stud_node *insertFront (struct stud_record *temp, struct stud_node *start);
struct stud_node *insertOthers(struct stud_record *temp, struct stud_node *start);
struct stud_node *delete(int target, struct stud_node *start);
struct stud_node *search(int target, struct stud_node *start);
struct stud_node *edit(struct stud_node *start);
struct stud_node *addData(struct stud_node *start);
struct stud_node *readFile(struct stud_node *);
struct stud_node *updateFile(struct stud_node *);
/////////////////////////////////////////////////////////////////////////////////////////////
// Main Program
main()
{
int choice;
int target=0;
//start pointer
struct stud_node *start;
start = NULL;
start = readFile(start);
do{
system("cls");
choice = mainMenu();
if(choice == 1){
system("cls");
start = addData(start);
printf("\n\n\n\nPress any key to continue...");
getch();
}
else if(choice == 2){
system("cls");
record_list(start);
printf("\n\n\n\nPress any key to continue...");
getch();
}
else if(choice == 3)
{
start=edit(start);
}
else if(choice ==4){
system("cls");
if(!isEmpty(start)){
printf("\n\nEnter Id to delete :");
scanf("%d", &target);
start = delete(target,start);
printf("\n\n\n\nPress any key to continue...");
getch();
}
else {
printf("\n\nRecord empty !\n\n");
printf("\n\n\n\nPress any key to continue...");
getch();
}
}
else if(choice==5)
{
printf("Enter Id to find :");
scanf("%d",&target);
search(target,start);
}
else if(choice==6){
updateFile(start);
system("cls");
}
else if(choice==7){
system("cls");
printf("System end !! ...\n\n");
break;
}
else{
printf("Error..Invalid Choice...!\n");
}
}while(choice >1 || choice <8);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Read data from binary file
//
struct stud_node *readFile(struct stud_node *start)
{
// declaration start
FILE *fptr;
struct stud_record temp;
int open_file_error = FALSE;
int end = FALSE;
int found = FALSE;
start = NULL;
//start read file and checking
fptr = fopen("Student.bin","rb");
if(fptr == NULL)
{
printf("Cannot open file...!\n");
open_file_error = TRUE;
}
while(!open_file_error && !end) // i will consider file not end of file
{
//record structure : size : date/read : file pointer
if (fread(&temp,sizeof(temp),1,fptr)==0)//read file untill finished
end=TRUE; // after finished make condition false and break out
else
//otherwise space unavailable goto sort data
start = insert(temp,start);
}
// showAll(start); // to test successful read
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Display function
//
void record_list(struct stud_node *head)
{
int lines=0,count=0;
int i=0;
title();
for(i=0; i<80; i++)
{
printf("=");
}
printf("No:\t Student Id\t Name \t\t \t Height Weight\n");
for(i=0; i<80; i++)
{
printf("=");
}
//when structure pointer space unavailable
while(head != NULL)
{
if (lines < 18)
lines++;
else
{
system("cls");
lines=0;
}
count++;
printf("%-4d\t %-10d\t %-25s %-8.2f\t %10.2f\n",
count,head->data.id,head->data.name,head->data.weight,head->data.height);
head = head->next;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
// List Title
//
void title(void)
{
spc(" << SMK High School >> \n");
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Screen space allocation
//
void spc(char text[])
{
int space = 0 , loop;
space = (80-(strlen(text)))/2;
for(loop=0;loop<space;loop++)
printf(" ");
printf("%s\n",text);
}
/////////////////////////////////////////////////////////////////////////////////////////////
// This function was used to check struture pointer condition
// and determine action to take
struct stud_node *insert(struct stud_record temp, struct stud_node *start)
{
// printf("%d %s %d %f\n",temp.id,temp.name,temp.qty,temp.price); //to test
// no record found, first adding start
if (start==NULL)
//&temp address passed into insertFirst() function
start = insertFirst(&temp,start);
else
//sort data by id
if (temp.id < start->data.id)
start = insertFront(&temp,start);
else
//record available, add other data
insertOthers(&temp,start);
return start;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Add first record
//
struct stud_node * insertFirst(struct stud_record *temp, struct stud_node *start)
{
//allocate memory to store data into start pointer structure
start = malloc(sizeof(temp)); // first size of memory allocated
//copy
strcpy(start->data.name,temp->name);
start->data.id = temp->id;
start->data.height= temp->height;
start->data.weight = temp->weight;
start->next = NULL;
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Add record at front
//
struct stud_node * insertFront(struct stud_record *temp, struct stud_node *start)
{
struct stud_node *newData;
newData = 0; //same as NULL
newData = malloc(sizeof(struct stud_record));
strcpy(newData->data.name,temp->name);
newData->data.id = temp->id;
newData->data.height= temp->height;
newData->data.weight = temp->weight;
newData->next = start;
start = newData;
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Add other data
//
struct stud_node * insertOthers(struct stud_record *temp, struct stud_node *start)
{
struct stud_node *currentPtr,*previousPtr,*newData;
currentPtr = previousPtr = newData = 0;
currentPtr = start;
//sort data at memory location untill all data in sequence
while(currentPtr != NULL && temp->id > currentPtr->data.id)
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
newData = malloc(sizeof(struct stud_record));
strcpy(newData->data.name,temp->name);
newData->data.id = temp->id;
newData->data.height= temp->height;
newData->data.weight = temp->weight;
newData->next = previousPtr->next;
previousPtr->next = newData;
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Add data process
struct stud_node *addData(struct stud_node *start)
{
int i;
int counter=0;
//part is structure variable
struct stud_record parts;//create a new record structure node
int continu_e = TRUE;
printf("Enter The Following Information: \n");
for(i=0; i<35; i++){
printf("=");
}
while(continu_e)
{
counter++;
parts.height=0;
parts.weight=0;
printf("\n\n%d.Enter student Name [Blank->return]?: ",counter);
fflush(stdin);
gets(parts.name);
if(strlen(parts.name)==0)
break;
printf("\nPlease Enter Student ID : ? ");
scanf("%d",&parts.id);
fflush(stdin);
printf("\nEnter Height : ? ");
scanf("%f",&parts.height);
fflush(stdin);
printf("\nEnter Weight : ? ");
scanf("%f",&parts.weight);
fflush(stdin);
start = insert(parts,start);
}
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Delete function
struct stud_node *delete(int target, struct stud_node *start)
{
struct stud_node *previousPtr, *currentPtr, *tempPtr;
//previousPtr=currentPtr =tempPtr=0;
if(target == start->data.id){
tempPtr = start;
start = start->next;
free(tempPtr);
return start;
}
else
{
previousPtr = start;
currentPtr = start->next;
//swap position
while(currentPtr != NULL && currentPtr->data.id != target)
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if(currentPtr !=NULL && currentPtr->data.id == target){
tempPtr = currentPtr;
previousPtr->next = currentPtr->next;
free(tempPtr);
printf("\n\n%d record deleted !\n\n", target);
return start;
}
}
printf("No such record ! ");
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Update function
struct stud_node *updateFile(struct stud_node *start)
{
FILE *fptr;
int end = FALSE;
struct stud_record data;
//open file to write
fptr = fopen("Student.bin", "wb");
if(fptr==NULL)
{
printf("Open file error !");
}
else
// fseek(fptr, sizeof(struct stud_record), 1, SEEK_SET);
while(!FALSE){
if(fwrite(&data, sizeof(struct stud_record), 1, fptr)==0){
end=TRUE;
}
else
break;
}
fclose(fptr);
// fwrite(&start->, sizeof(struct stud_record), 1, fptr);
getch();
return start;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Search Record Function
struct stud_node *search(int target, struct stud_node *start)
{
struct stud_node *previousPtr, *currentPtr;
previousPtr=currentPtr=0;
if(target == start->data.id){
system("cls");
printf("\nRecord Found !\n");
printf("================\n");
printf("Name : %s\n", start->data.name);
printf("ID : %4d\n", start->data.id);
printf("Name : %.2f\n", start->data.weight);
printf("Name : %.2f\n", start->data.height);
getch();
return start;
}
else
{
previousPtr = start;
currentPtr = start->next;
//swap position
while(currentPtr != NULL && currentPtr->data.id != target)
{
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if(currentPtr !=NULL && currentPtr->data.id == target){
system("cls");
printf("\nRecord Found !\n");
printf("================\n");
printf("Name : %s\n", currentPtr->data.name);
printf("ID : %4d\n", currentPtr->data.id);
printf("Name : %.2f\n", currentPtr->data.weight);
printf("Name : %.2f\n", currentPtr->data.height);
getch();
return start;
}
system("cls");
printf("No Such Record !");
getch();
return start;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Check Pointer
int isEmpty(struct stud_node *start)
{
return start == NULL;
}
/////////////////////////////////////////////////////////////////////////////////////////////
struct stud_node *edit(struct stud_node *start)
{
int id;
printf("Enter Id to edit record :");
scanf("%d", &id);
getch();
return start;
}
/////////////////////////////////////////////////////////////////////////////////////////////
|