[ back to toc ]

files & arrays

Date: 2002/05/12 19:38

Q:
hi,
i am stuck here with my program coz when i compile it i get no errors but
when i run it,the whole system shuts down!!!! can you help me to find my
big mistake in the program???plz!

NOTE:the porpose of my program is to use an array to index the file ,to
make it easeir but it is all getting harder for me!

#include<stdio.h>
////////////////////////////////////////////
typedef struct{
int accNum;
char name[30];
double balance;
char used;} Account;

//****************************//
typedef struct{
int accNum;
long offset;}index;

index myIndex[300];

/////////////////////////////////////////////////////////

int erase( FILE * fptr,index A[]){
Account a;
long offset;
int i,j,count,found_it=0;

printf("\nEnter number of account to be deleted: ");
scanf("%i", &a.accNum);
count=index_fill(A,fptr);
for(i=0;i<count;i++)
if(a.accNum==A[i].accNum){
found_it=1;
break;
}
if(found_it==0)
return(0);
a.accNum = -1;
a.used = 'n';

offset=A[i].offset;
fseek( fptr, offset, SEEK_SET);
fwrite( &a, sizeof(Account), 1, fptr);

//now the for loop to del this elemnt from the array
for(j=i;j<count-1;j++)
A[j]=A[j+1];
return(1);
}
////////////////////////////////////////////
int search (FILE * fptr, int num, long * offset){

Account a ={0 , "" , 0.0};

rewind(fptr);
*offset = ftell(fptr);
fread(&a, sizeof(Account), 1, fptr);
while ( !(feof(fptr)) && (a.accNum != num) ){
*offset = ftell(fptr);
fread(&a, sizeof(Account), 1, fptr);
}
if (a.accNum == num)
return(1);
return(0);
}
////////////////////////////////////////////
int insert (FILE * fptr,index A[]){

Account a;
long offset;
int count,i;

printf("\nEnter number of account to be inserted: ");
scanf("%i", &a.accNum);
if(a.accNum<1){
printf("\n account number must be greater than 0,your insertion
was cancled\n");
return(0);
}

count=index_fill(A,fptr);
for(i=0;i<count;i++)
if(a.accNum==A[i].accNum){
printf("this account number already exists!\n");
return(0);
}

printf("\nEnter name: ");
scanf("%s", a.name);
printf("\nEnter balance: ");
scanf("%lf", &a.balance);
a.used='y';
count=index_fill(A,fptr);//this statement is done to add
//the new record to the array.
fseek(fptr,offset,SEEK_SET);
fwrite(&a, sizeof(Account), 1, fptr);
count=index_fill(A,fptr);
return(1);
}
////////////////////////////////////////////
int index_fill (index A[],FILE *f1){

int recCount=0,k,j;
long offset;
Account partRec;
index temp;

rewind(f1);
offset=ftell(f1);
fread(&partRec,sizeof(Account),1,f1);

while(!feof(f1)){

A[recCount].offset=offset;
A[recCount++].accNum=partRec.accNum;
offset=ftell(f1);
fread(&partRec,sizeof(Account),1,f1);
}

for(k=1;k<=recCount-1;k++)
for(j=0;j<=recCount-2;j++)

if(A[j].accNum>A[j+1].accNum){
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}

return recCount;
}
/////////////////////////////////////////////////
void display(FILE * fptr,index A[]){
Account a;
int count,i;

printf("\n%-20s %-30s %-20s\n", "Account Number", "Name",
"Balance");
rewind(fptr);
count=index_fill(A,fptr);

for(i=0;i<count;i++){
fseek(fptr,A[i].offset,SEEK_SET);
fread(&a,sizeof(Account),1,fptr);
printf("%-20i %-30s %-20lf\n", a.accNum, a.name, a.balance);
}
}

/////////////////////////////////////////////////////////////////
int clean(FILE * fptr, char * fname){

FILE * tempfptr;
Account a;

if ( !(tempfptr = tmpfile()) ){
printf("\nCannot create a temp file! The cleaning process is
aborted");
return(0);
}
rewind(fptr);
fread(&a, sizeof(Account), 1, fptr);
while ( !feof(fptr) ){
if (a.used == 'y')
fwrite(&a, sizeof(Account), 1, tempfptr);
fread(&a, sizeof(Account), 1, fptr);
}
fclose(fptr);
if ( !(fptr = fopen(fname, "wb+")) ){
printf("\nError, cannot delete the unused records, cleaning
process is aborted.");
return(0);
}
rewind(tempfptr);
fread(&a, sizeof(Account), 1, tempfptr);
while ( !feof(tempfptr) ){
fwrite(&a, sizeof(Account), 1, fptr);
fread(&a, sizeof(Account), 1, tempfptr);
}
fclose(tempfptr);
return(0);
}
//////////////////////////////////////////////////////////////////////////
////////////////////////////////////
void update(FILE * fptr){

Account a;
long offset;

printf("\nEnter number of account to be updated: ");
scanf("%i", &a.accNum);
if ( !search(fptr, a.accNum, &offset) ){
printf("\nThis account does not exists!\n");
return;
}
printf("\nEnter name: ");
scanf("%s", a.name);
printf("\nEnter balance: ");
scanf("%lf", &a.balance);
fseek(fptr,offset,SEEK_SET);
fwrite(&a, sizeof(Account), 1, fptr);
}
////////////////////////////////////////////
int menu(){

int choice;

printf("\n1) Insert a record.\n");
printf("2) Update an account.\n");
printf("3) Display All records.\n");
printf("4) delete a record.\n");
printf("5) clean the updates.\n");
printf("6) Quit.");
do{
printf("\nEnter your choice: ");
scanf("%i", &choice);
} while (choice < 1 || choice > 4);
return(choice);
}
////////////////////////////////////////////
int main(){

FILE *fptr;
char fname[30];
int choice;

printf("\nEnter Database file name: ");
scanf("%s", fname);
if ( !(fptr = fopen(fname,"rb+")) ){
printf("%s Could not be opened, it might be not existing.%s", fname,
"\nEnter 1 to create it, 0 to stop the program\n");
scanf("%i",&choice);
if (choice && (fptr = fopen(fname,"wb+")) )
printf("%s has been created\n", fname);
else{
if (choice && !fptr)
printf("%s could not be created!!\n", fname);
printf("The program is terminating \n");
return(1);
} // else
} //if

do{
choice = menu();
switch(choice){
case 1: insert(fptr,myIndex);
break;
case 2: update(fptr);
break;
case 3: display(fptr,myIndex);
break;
case 4: erase(fptr,myIndex);
break;
case 5: clean(fptr,fname);
break;
}
} while (choice != 6);
fclose(fptr);
} // main

A:
Sorry, but I can not take the job to debug your code. However I suggest
that you run your program on a machine that utilizes some operating system
(I mean some operating system and not DOS, Wind95 or alike) and run the
code in debugger. This will point out sooner where the problem is.

Regards,
Peter

[ back to toc ]