How to change a specific entity of a specific record stored in a file in c++ -
hi friends want modify specific entity of specific record stored in file. have stored record in file as
roll no: 35
name: adnan irshad
status: not admitted
roll no: 40
name: adeel ahmed
status: not admitted
roll no: 30
name: arish
adnan: not admitted
roll no: 35
name: shan
status: not admitted
here want search record via roll no , change status of record admitted input 30 roll no search record , program search record , change status admitted. have done code change 1 record when record nothing happen. code here
char stat[13]; cout<<"enter registration no"; cin>>reg_no; fstream out("intermediate.txt",ios::in|ios::out|ios::ate|ios::app); out.seekg(0,ios::beg); out.read((char*)&std,sizeof(std)); while(!out.eof()) { record_no++; if(strcmp(std.get_reg_no(),reg_no)==0) { found=1; break; } } location=(record_no-1)*sizeof(std); out.seekp(location,ios::beg); char new_status[]="addmitted"; // initialized replace status strcpy(std.status,new_status); out.write((char*)&std,sizeof(std)); out.close();
to modify text file:
- load file memory (such
std::vector<my_data_struct>
) - modify data in memory
- write memory file
you making significant mistakes costing you:
- don't use
goto
; there better way (subfunctions) - don't loop on eof
- use
std::string
instead ofchar*
- modularize, modularize, modularize
that last point needs additional help: 1 thing @ time. main code should like:
get user input (filename, item modify, etc) std::vector<items> items = load_file( filename ); auto item = std::find( items.begin(), items.end(), my_search_criterion_predicate ); if (item != items.end()) { modify item; } write_file( items ); user output
all of these require little learning on part, worth effort.
Comments
Post a Comment