hi guys, here's my code im having a hard time on how to edit. i can add and view it yet unable to edit it.

thanks!

/*
 * dental.c
 *
 *  Created on: 30/04/2010
 *      Author: danovert
*/
#include<stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
typedef struct {
char fname[30];
char lname[50];
char dental[30];
}entry;
char fname[30];char lname[50];
FILE *fpt;int menu(void);
entry *ptr, name;
int n=sizeof(entry);char ch,y;
void edit(entry *);
void main()
{
int choice;
do
{
choice=menu();
switch(choice)
{
case 1:ptr=(entry *)malloc(sizeof(entry));
fpt=fopen("data5.bin","a");
printf("\nEnter given name:");
scanf("%s",ptr->fname);
printf("\nEnter the name of the last name:");
scanf("%s",ptr->lname);
printf("\nEnter the dental needs:");
scanf("%s",ptr->dental);
fwrite(ptr,n,1,fpt);printf("ENTERED\n");
fclose(fpt);
continue;
case 2:ptr=(entry *)malloc(sizeof(entry));
fpt=fopen("data5.bin","r+");
do
{
if (fread(&ptr->fname, sizeof(ptr->fname), 1, fpt) == 1 &&
fread(&ptr->lname, sizeof(ptr->lname), 1, fpt) == 1 &&
fread(&ptr->dental, sizeof(ptr->dental), 1, fpt) == 1)

{

printf("\n\nGiven name:");
printf("%s",ptr->fname);
printf("\nFamily name:");
printf("%s",ptr->lname);
printf("\nDental Needs:");
printf("%s",ptr->dental);
printf("\nWant to EDIT this entry?(Enter y/n):");
ch=getchar();
if(ch==y)
edit(ptr);
}
else if (feof(fpt))
{
printf("\n\nfinished reading file!\n");
break;
}
else {
printf("an error occured while reading the file!\n");
break;
}
} while (1);
fclose(fpt);continue;
case 3:printf("\nThank You for using!");exit(1);
}
}while(choice==1||choice==2);
getch();
}
int menu(void)
{
int choice;
do{
	printf("\n \n \n \n \n \n Dental Clinic - Orthodontics \n\n\n\n\n\n");
printf("Enter your choice:\n");
printf("1.Enter Patient's Name\n");
printf("2.Display and Edit Patient's Name\n");
printf("3.EXIT\n");
scanf("%d",&choice);
}while(choice!=1 && choice!=2 && choice!=3);
return(choice);
}
void edit(entry *ptr)
{
printf("\nWant to change given name?(y/n):");
ch=getchar();
if(ch==y)
{
printf("\nEnter new given name:");
scanf("%s",fname);
printf("ptr->fname=fname");
}
printf("\nWant to change family name?(y/n):");
ch=getchar();
if(ch==y)
{
printf("\nEnter new company name:");
scanf("%s",lname);
printf("ptr->lname=lname");
}
fwrite(ptr,n,1,fpt);
return;

}

When editing you need to reposition the file pointer to the beginning of the record you just read before writing, otherwise you overwrite the next record in the file. You will need to use fseek to do this.

Additionally you program is leaking memory like a sieve, every time you call malloc you need to be calling free once you have finished with the memory to return it to the heap.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.