0

this is wat i hav been doing but its not workin.i am not able to write the code for searching

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
//***Record structure declaration***
struct Details
{
char name[30];
char sex;
char status;
char title;
char address[40];
char qualification[10];
char experience[20];
int telephoneno;
int membershipno;
int subdate;
int paymentdate;
int fee;
int dateofbirth;

}chefs[20];
//***Prototype for reading a record from keyboard function***

void read_data();

void write_data();

void ReadDetails(Details&);

void main()
{
char ch;
cout<<"enter your choice w for writing a record and r for reading a record from the file"<<endl;
cin>>ch;
switch(ch)
{
case'w':
write_data();
break;
case'r':
read_data();
break;
default:
cout<<"error";
}
getch();
}

void write_data()

{

FILE*testfile;
Details det;
//***Open binary file for writing***
testfile=fopen("C:\\test.dat","wb");
//***If file not found,error message***
if(!testfile)
{
cout<<"error opening file";
exit(1);
}
//**Read a record's details from the keyboard***


ReadDetails (det);
//***WHILE not terminate message DO***
while (strcmp(det.name,""))
{
//***Write data to file***
fwrite(&det,sizeof(det),1,testfile);
//***Read more details from the keyboard***
ReadDetails(det);
}
fclose(testfile);
}
//***Read record from the keyboard function***

void ReadDetails(Details&d)
{
//***Get data from keyboard***
clrscr();
cout<<"enter your name";
cin>>d.name;
cout<<"enter sex;m for male or f";
cin>>d.sex;
cout<<"enter your status;l for licentiate,a for associate,m for member or f for fellow";
cin>>d.status;
cout<<"enter your title";
cin>>d.title;
cout<<"enter your address";
cin>>d.address;
cout<<"enter your qualification";
cin>>d.qualification;
cout<<"enter your experience";
cin>>d.experience;
cout<<"enter your telephoneno";
cin>>d.telephoneno;
cout<<"enter your membershipno";
cin>>d.membershipno;
cout<<"enter your subdate";
cin>>d.subdate;
cout<<"enter your fee";
cin>>d.fee;
cout<<"enter your paymentdate";
cin>>d.paymentdate;
}

void read_data()

{
FILE*testfile;
Details p;
//***Open binary file for reading***
testfile=fopen("C:\\test.dat","rb");
if(!testfile)
//***If file not found,error message***
{
cout<<"error opening file\n";
exit(1);
}
clrscr();
cout<<"THE FILE CONTAINS THE FOLLOWING DATA\n\n";
//***Read data from file***
while(fread(&p,sizeof(p),20,testfile)==20)
{
//***Display data read***
cout<<p.name<<' '<<p.sex<<' '<<p.status<<' '<<p.title<<' '<<p.address<<' '<<p.qualification<<' '<<p.experience<<' '<<p.membershipno<<' '<<p.subdate<<' '<<p.paymentdate<<' '<<p.fee<<' '<<p.dateofbirth<<endl;
getch();
}
cout<<"End of file reached\n";
//***Close file***
fclose(testfile);

getch();
};

add_record();

modify_record();

delete_record();

void main()

{
char ch;
cout<<"enter 'd'for deletion";
cin>>ch;
cout<<"enter 'a' for adding";
cin>>ch;
cout<<"enter 'm'for modification";
switch (ch)
{
case'd':
delete_record();
break;
case 'a':
add_record();
break;
case'm':
modify_record();
break;
}//switch finish

}//main finish

modify_record()

{
FILE*testfile;
Details p;
//***Open binary file for updating***
testfile=fopen("C:\\test.dat","r+b");
//***If file not found,error message***
if(!testfile)
{
cout<<"error opening file\n";
exit(1);
}
clrscr();
//***Calculate how many records are in the file***
fseek(testfile,0,20);
int noofrecs=ftell(testfile)/sizeof(p);
cout<<"The file contains"<<noofrecs<<"records\n";
fseek(testfile,0,0);
//***Read record position***
cout<<"Enter record required (-1 to stop):";
int post;
cin>>post;
//***WHILE not terminate option DO
while(post!=-1)
{
//***Seek required record***
fseek(testfile,sizeof(p)*(post-1),0);
//***IF record read from file THEN***
if (fread(&p,sizeof(p),1,testfile)==1)
{
//***Display data read***
cout<<p.name<<' '<<p.sex<<' '<<p.status<<' '<<p.title<<' '<<p.address<<' '<<p.qualification<<' '<<p.experience<<' '<<p.membershipno<<' '<<p.subdate<<' '<<p.paymentdate<<' '<<p.fee<<' '<<p.dateofbirth<<endl;
//***Get replacement fields***
cout<<"Enter the new name:";
cin>>p.name;
cout<<"Enter the new sex:";
cin>>p.sex;
cout<<"Enter the new status:";
cin>>p.status;
cout<<"Enter the new title:";
cin>>p.title;
cout<<"Enter the new address:";
cin>>p.address;
cout<<"Enter the new qualification:";
cin>>p.qualification;
cout<<"Enter the new experience:";
cin>>p.experience;
cout<<"Enter the new membershipno:";
cin>>p.membershipno;
cout<<"Enter the new subdate:";
cin>>p.subdate;
cout<<"Enter the new paymentdate:";
cin>>p.paymentdate;
cout<<"Enter the new fee:";
cin>>p.fee;
cout<<"Enter the new dateofbirth:";
cin>>p.dateofbirth;
//***Move file pointer to record just read***
fseek(testfile,(post-1)*sizeof(p),0);
//***Rewrite the record to the file***
fwrite(&p,sizeof(p),1,testfile);
}
else
//***ELSE display error message***
{
cout<<"Invalid record number.Press any key\n";
getch();
}
//***Read record position***
cout<<"Enter record required (-1 to stop):";
cin>>post;
}
cout<<"Program terminated\n";
//***Close file***
fclose(testfile);
getch();
return 0;
}

delete_record()

{
//***Open files***
FILE *infile, *outfile;
Details sd;

infile=fopen("c:\\test.dat","rb");
if(!infile)
{
cout<<"error opening file";
exit(1);
}
int record;
cout<<"which record you want to delete 1,2, 3 or 4";
cin>>record;
outfile=fopen("c:\\temp.dat","wb");
int count=1;
while(fread(&sd,sizeof(sd),1,infile)==1)
{
if(count!=record)
{
fwrite(&sd,sizeof(sd),1,outfile);

}
count++;
}
//while end**
fclose(infile);
fclose(outfile);
system("DIR");
//***Delete old file***
system("del test.dat");
//***Rename new file***
system("REN temp.dat test.dat");
cout<<"program finish normally";
getch();
return 0;
}

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Lerner
0

Welcome to DaniWeb. Please repost using code tags. Almost nobody ever does it the first time around, but it is nice when you do.

A few quick observations.

1) main() should return type int, not void, even if your compiler allows the return type void syntax, if for no other reason than it's one less char to type. The real trajedy however is that you delcare main() twice. A program can only have one main().

2) With the declaration of your struct the compiler might be able to figure out the maximum size of a struct to be used with fwrite() and fread() but each object may have varying actual size because each of the strings may be less than the capacity of the memory declared. If you want to do this with strings you could try padding so each string has exactly the same size just like each int and each double and each float has exactly the same size. Alternatively, you could overload the << and the >> operators for the struct and within the declaration of the overloaded operators do serial read/write of each member of the struct, thus allowing each struct object to be of variable size, if you want. If you did that, then you would need to read the whole file into memory, change what you want, and write the changed file contents back to file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.