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(,""))
{
//***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;
}

Dani AI

Generated

Short summary tied to the thread: 's program mixes C-style binary I/O and fixed-size C strings and contains several common bugs; correctly points out the duplicate main() and the design choice between fixed-size (random-access) records and serialized/variable-size records. The practical choices are: (A) make every record a fixed byte size so a single record can be sought and overwritten in-place, or (B) keep variable-length text/serialized records and load/modify/write the file as a whole (or maintain an index).

Concrete problems seen in the posted code and how to fix them:

  • Duplicate main() — a program must have exactly one entry point; prefer int main().
  • Wrong fseek/fread usage — use SEEK_SET/SEEK_END and call fread(ptr, sizeof(rec), 1, f) and check for == 1.
  • Writing raw structs that include text buffers is fragile: compiler padding and field sizes make the binary non-portable. Either fix field widths precisely or serialize each field explicitly.
  • Use std::string and std::vector and std::fstream for safer, clearer code; treat phone numbers and dates as strings or structured types (not plain int).
  • For deletion either mark records with a tombstone flag or copy all non-deleted records to a temporary file and rename it.

Minimal examples of the two patterns (illustrative only):

/* fixed-size record: random-access update */
fseek(f, 0, SEEK_END);
long size = ftell(f);
size_t count = size / sizeof(Record);
fseek(f, index * sizeof(Record), SEEK_SET);
if (fread(&rec, sizeof(rec), 1, f) == 1) {
    /* modify rec fields */
    fseek(f, index * sizeof(rec), SEEK_SET);
    fwrite(&rec, sizeof(rec), 1, f);
}
/* variable-length approach: load-modify-save */
std::vector<Record> all = load_all_records();
auto it = std::find_if(all.begin(), all.end(), [&](const Record& r){ return r.id == key; });
if (it != all.end()) { it->phone = newPhone; save_all_records(all); }

Practical tips: always check file-open and I/O return values, avoid non-standard conio.h functions for portable code, reserve enough bytes for fixed fields (or choose serialization), and prefer rewriting the file after in-memory edits unless fixed-size records and true random-access updates are required. 's suggestion to either pad fields or implement field-by-field serialization is the right architectural choice before adding search/update logic.

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.

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.