File manipulation

hello
ive been doing this program and i can't seem to figure out how to delete a record file. like if you delete record 2 from a 4 records database, record 3 should overlap record 2 and the last record should adjust. please help. thanks

by the way, use Dev C++ to run this program.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
# define INDEX 10 //there are 9 records in a masterfile (temporary)

typedef struct fruit
{
int code;
char name[20];
} fruit;
fruit fruits[9];
//dummy files
typedef struct d_fruit
{
int d_code;
char d_name[20];
} d_fruit;
d_fruit d_fruits[9];
FILE *fruit_p, *d_fruit_p, *countrecord, *record_p;

void masterfile(); // name of a function masterfile
void displayfile(); //name of a function displayfile
void searchrecords(); //name of a function searchfile
void deleterecords(); //name of a function deleterecords
int choice,i, recordcounter;
int buffer_size;
int main()
{

do
{
system ("cls"); //clear the screen
printf("\n\n\n"); //three enter key
printf(" \t M E N U \n\n");
printf("\t1. Create a Master File \n");
printf("\t2. Search from Master File \n");
printf("\t3. Delete a Record from Master File \n");
printf("\t4. Update a Record from Master File \n");
printf("\t5. Display a Record from Master File \n");
printf("\t6. Quit \n");
printf("\n\nSelect a number : \n\n");
scanf("%d", &choice);
fclose(fruit_p);
fclose(countrecord);
switch (choice)
{
case 1:
system ("cls");
//printf("you have selected Create a Master File \n");
//system ("pause"); //screen pause
masterfile();
break;
case 2:
system ("cls");
//printf("you have selected Search a Record a Master File \n");
//system ("pause"); //screen pause
searchrecords();
break;
case 3:
system ("cls");
//printf("you have selected Delete a Record from Master File \n");
//system ("pause"); //screen pause
deleterecords();
break;
case 4:
system ("cls");
printf("you have selected Update a Record from a Master File \n");
system ("pause"); //screen pause
break;
case 5:
system ("cls");
//printf("you have selected Display a Record from a Master File \n");
//system ("pause"); //screen pause
displayfile();
break;
case 6:
break;
} //end switch
}while (choice != 6);//end do
//system ("pause"); //screen pause
} //end of main

//this is the masterfile function
void masterfile()
{
char sel, ans;
int a;
system("cls");
printf("\n\nMASTER FILE \n");
printf("\n Note: Creating a Master file can delete the orignial\n");
printf("records of your masterfile\n");
printf("\n Continue anyway [Y/N] : ");
sel = getche();
if (sel == 'N' || sel == 'n')
{
//return to main menu
}
else
{
a=0; //count how many fruits were entered

fruit_p = fopen("\master.txt","w");

do
{
system("cls");
printf("Master File Report\n");
printf("Enter Code: ");
scanf("%d", &fruits[a].code);
printf("Enter Name : ");
scanf("%s", fruits[a].name);

printf("\n\n\nWoud you like to continue [Y/N]? ");

ans = getche();
if((fruit_p = fopen("\master.txt","w"))!=NULL)
{
fwrite(fruits, sizeof(struct fruit), 1, fruit_p);
}
fclose(fruit_p);
a++;
}while(ans !='N'); //end of do-while statement
ecordcounter = a;
}//end of if statement
fclose(fruit_p); //closes the file master.txt
//for purpose of getting how many records entered
countrecord = fopen("\cntrec.txt","w");
fscanf(countrecord,"%d", &recordcounter);
fprintf(countrecord,"%d", recordcounter);
fclose(countrecord);
}//end of masterfile

void displayfile()
{
system("cls");
printf("DISPLAY RECORDS\n");
printf("\n\n\nCODE\t\tNAME\n");
fruit_p = fopen("\master.txt","r");
countrecord = fopen("\cntrec.txt","r");

if(fruit_p!=NULL)
{
fprintf(record_p,"%d", recordcounter);
fscanf(record_p,"%d", &recordcounter);
fread(fruits, sizeof(struct fruit), recordcounter, fruit_p);
}//end of if-statement

for(i=0;i<recordcounter;i++) //
{
printf("%d\t\t%s\n", fruits[i].code, fruits[i].name);
}//end of for-loop
fclose(fruit_p);
fclose(record_p);
system("pause");
}//end of displayfile

void searchrecords()
{
}//end of searchrecords

void deleterecords()
{
int a, c;
int delcode;
system("cls");
printf("DELETING RECORDS\n");
printf("\n\nPlease indicate code to delete: ");
scanf("%d",&delcode);

fruit_p = fopen("\master.txt","r");
c = getc(fruit_p); //to read until the end of the file
countrecord = fopen("\cntrec.txt","r");
fscanf(countrecord, "%d", &recordcounter);

if(fruit_p!=NULL)
{
fread(fruits, sizeof(struct fruit), recordcounter, fruit_p);
}//end of if- statement;
for (i=0;i<recordcounter;i++)
{
if(delcode == fruits[i].code)//if match
{
d_fruits[i].d_code = fruits[i+1].code;
strcpy(d_fruits[i].d_name, fruits[i+1].name);
}
else
{
d_fruits[i].d_code = fruits[i].code;
strcpy(d_fruits[i].d_name, fruits[i].name);
}
//end of if=statement
}//end of for loop
fclose(fruit_p);
d_fruit_p = fopen("\dummy.txt","w");
if(d_fruit_p!=NULL)
{
fwrite(d_fruits, sizeof(struct d_fruit), recordcounter, d_fruit_p);
}//end if-statement
fclose(d_fruit_p);
fruit_p = fopen("\master.txt","w");
d_fruit_p = fopen("\dummy.txt","r");

for (i=0;i<recordcounter-1;i++)
{
fruits[i].code = d_fruits[i].d_code;
strcpy(fruits[i].name, d_fruits[i].d_name);
}//end of for loop

if(fruit_p!=NULL)
{
fwrite(fruits, sizeof(struct fruit), recordcounter, fruit_p);
}//end if-statement
fclose(fruit_p);
fclose(d_fruit_p);
fclose(countrecord);
printf("\n\n\nRecords deleted ...\n");
system("pause");
}//end of delete records
can you please use for code tag for us, for easily tracking your program down...
I'm sorry, this is my first time to use this forum so i don't know how to tag, but i will thy to explain my problem.

this will ask the user to input records and saved in MASTER.TXT
let's say in my Master.txt, i have entered this records:
CODE NAME
111 Apple
222 Grapes
333 Pears


I'm having problems with the deletion part
I used two text file here : MASTER.TXT and DUMMY.TXT

fruit_p = fopen("\master.txt","r");
c = getc(fruit_p); //to read until the end of the file
.....
d_fruit_p = fopen("\dummy.txt","w");
if(d_fruit_p!=NULL)
{
fwrite(d_fruits, sizeof(struct d_fruit), recordcounter, d_fruit_p);
}//end if-statement


the idea is when i search a record to delete, i will use the code to search into my MASTER.TXT
printf("\n\nPlease indicate code to delete: ");
scanf("%d",&delcode);

fruit_p = fopen("\master.txt","r");


and when the record is found, I will save its content to my DUMMY file replacing that record with the value of the next record. And when end of file is reached, return the contents of the DUMMY File to the master file:

if(delcode == fruits[i].code)//if match
{
d_fruits[i].d_code = fruits[i+1].code;
strcpy(d_fruits[i].d_name, fruits[i+1].name);
}
....
d_fruit_p = fopen("\dummy.txt","w");
if(d_fruit_p!=NULL)
{
fwrite(d_fruits, sizeof(struct d_fruit), recordcounter, d_fruit_p);
}//end if-statement
fclose(d_fruit_p);
fruit_p = fopen("\master.txt","w");
d_fruit_p = fopen("\dummy.txt","r");

for (i=0;i<recordcounter-1;i++)
{
fruits[i].code = d_fruits[i].d_code;
strcpy(fruits[i].name, d_fruits[i].d_name);
}//end of for loop

if(fruit_p!=NULL)
{
fwrite(fruits, sizeof(struct fruit), recordcounter, fruit_p);
}//end if-statement
fclose(fruit_p);
fclose(d_fruit_p);
fclose(countrecord);
printf("\n\n\nRecords deleted ...\n");
system("pause");
}//end of delete records


However, if i will try to display the records, here is the result
Let's say I'm going to delete record 2 which is "GRAPES"


CODE NAME
111 Apple
333 Pears
333 Pears

I can't make it to work like this

CODE NAME
111 Apple
333 Pears


I hope i was able to explain my concern.

Thank you for your help.


Topic archived. No new replies allowed.