c++ - Binary structure delete error -
this whole program. right now,the delete function not write temp file. i'll try change str array string , see if helps.
the program supposed read write edit search , sort binary file structures.
#include <cstdlib> #include <stdlib.h> #include <iostream> #include <fstream> #include<string> #include<limits> #include<iomanip> #include<stdio.h> #include <errno.h> using namespace std; const int size=40; char typedef str[size]; char fname[]="c:/solident.dat"; char tname[]="c:/temp.dat"; struct rec{int id;str lname,fname;}; //prototypes void menu(string&); void engine(string&,rec&,fstream&,fstream&,int,char[],char[]); void openfile(char[]); void addrec(rec&,char[]); void delall(char[]); void listall(rec&,fstream &,char[]); void apprec(rec&,fstream &,char[]); void delrec(rec&,fstream &, fstream &,char[],char []); void srcrec(rec&,fstream& ,char[]); void modrec(rec &j,fstream &outfile, char fname[]); void sort(); int main(int argc, char *argv[]) { string choice; rec student; fstream infile; fstream outfile; { menu(choice); engine(choice,student,infile,outfile,size,fname,tname); } while(choice[0]!='0'); system("pause"); return exit_success; } //function menu void menu(string &choice) { cout<<"\n\n\n\t\t========================"; cout<<"\n\n\t\tmenu \n"; cout<<"\n\t\t========================\n\n"; cout<<"\t\t[1] open/check file\n"; cout<<"\t\t[2] add records\n"; cout<<"\t\t[3] append records\n"; cout<<"\t\t[4] search record\n"; cout<<"\t\t[5] delete record\n"; cout<<"\t\t[6] delete records\n"; cout<<"\t\t[7] modify record\n"; cout<<"\t\t[8] list records\n"; cout<<"\t\t[9] sort records\n"; cout<<"\t\t[0] exit\n"; cout<<"\n\t\t========================= \n"; cout<<"\t\tyour choice :"; getline(cin,choice); while (choice[0] !='1' && choice[0] !='2' &&choice[0] !='3' &&choice[0] !='4' &&choice[0] !='5' &&choice[0] !='6' &&choice[0] !='7' &&choice[0] !='8' &&choice[0] !='9' &&choice[0] !='0' ) { cout<<"\n\t\t"<<choice<<" invalid!"; cout<<"\n\t\tenter correct choice: "; getline(cin,choice); } cout<<"\n\t\t========================= \n\n"; } //function void engine(string &choice,rec &j,fstream &infile,fstream &outfile,int size,char fname[],char tname[]) { switch (choice[0]){ case '1': { cout<<"executing number "<<choice<<":"<<"open/check file\n"<<endl; openfile(fname); break; } case '2': { cout<<"executing number "<<choice<<":"<<"add records\n"<<endl; addrec(j,fname); break; } case '3': { cout<<"executing number "<<choice<<":"<<"append records\n"<<endl; apprec(j,outfile,fname); break; } case '4': { cout<<"executing number "<<choice<<":"<<"search record\n"<<endl; srcrec(j,infile,fname); break; } case '5': { cout<<"executing number "<<choice<<":"<<"delete record\n"<<endl; delrec(j,infile, outfile,fname,tname); break; } case '6': { cout<<"executing number "<<choice<<":"<<"delete records\n"<<endl; delall(fname); break; } case '7': { cout<<"executing number "<<choice<<":"<<"modify record\n"<<endl; modrec(j,outfile,fname); break; } case '8': { cout<<"executing number "<<choice<<":"<<"list records\n"<<endl; listall(j,infile,fname); break; } case '9': { cout<<"executing number "<<choice<<":"<<"sort records\n"<<endl; sort(); break; } default:exit(0); } } //function void openfile(char fname[]) { cout<<"\nopening file"<<fname<<"..."<<endl; //confirm if file exists , opens fstream infile; fstream outfile; infile.open(fname,ios::in|ios::binary); if (infile.is_open()) { cout<<"file "<<fname<<" exists, , opened succesfully."; } infile.close(); //if fail ask create if (infile.fail()) { cout<<"file not exist. create? [y]/[n]"; char ch; cin>>ch; cin.ignore(numeric_limits<streamsize>::max(), '\n'); //if yes create file if(ch=='y'|| ch=='y') outfile.open(fname,ios::out| ios::binary); //if not created display error if (outfile.fail()) { cout<<"error: file not created"; } //else confirm creation of file cout<<"file "<<fname<<" has been created.\n\n"; outfile.close(); } } //function void addrec(rec&j,char fname[]) { fstream outfile; outfile.open(fname,ios::out|ios::binary|ios::app); char another[10]; { cout<<"please enter id number :"; cin>>j.id; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<"\nplease enter first name: "; cin.getline(j.fname,size); cout<<"\nplease enter lastname: "; cin.getline(j.lname,size); outfile.write((char*) &j, sizeof(j)); cout<<"add record:[y]/[n] "; cin.get(another[0]); cin.ignore(numeric_limits<streamsize>::max(), '\n'); } while(another[0]=='y'||another[0]=='y'); cout<<"exiting main menu.."<<endl; } //function void delall(char fname[]) { fstream outfile; cout<<"\n------------------\n"<<endl; cout<<"\ndelete records"<<fname<<"..."<<endl; cout<<"\nare sure want delete records in "<<fname<<" [y]/[any key exit]?"<<endl; char ch; cin>>ch; cin.ignore(numeric_limits<streamsize>::max(), '\n'); if(ch=='y'||ch=='y') { outfile.open(fname,ios::out|ios::binary|ios::trunc); outfile.close(); } else {cout<<"\nexiting main menu..."<<endl;} } //function void listall(rec&j,fstream &infile,char fname[]) { infile.clear(); infile.open(fname,ios::in|ios::binary); infile.read((char*)&j,sizeof(j)); cout<<"|=======================================================|"<<endl; cout<<"| # | id | first | last |"<<endl; cout<<"|=======================================================|"<<endl; int count=1; while(!infile.eof()) { cout<<"| "<<setw(6)<<left<<count<<left<<"| "<<setw(9)<<j.id<<"| "; cout<<left<<setw(14)<<j.fname<<"| "; cout<<left<<setw(19)<<j.lname<<"|"<<endl; infile.read((char*)&j,sizeof(j)); count=count+1; cout<<"|-------------------------------------------------------|"<<endl; } infile.close(); } //function void apprec(rec &j,fstream &outfile, char fname[]) { outfile.open(fname,ios::out|ios::binary|ios::app); outfile.clear(); cout<<"please enter id number :"; cin>>j.id; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<"\nplease enter first name: "; cin.getline(j.fname,size); cout<<"\nplease enter lastname: "; cin.getline(j.lname,size); outfile.write((char*) &j, sizeof(j)); outfile.close(); } //---------------------------------------------------- void delrec(rec&s,fstream &infile, fstream &outfile,char fname[],char tname[]) { int current, request=1; infile.open("c:/solident.dat",ios::in|ios::out|ios::binary); if (!infile.is_open()) cout << "infile not open\n"; outfile.open("c:/temp.dat",ios::out|ios::binary|ios::trunc); if (!outfile.is_open()) cout << "outfile not open\n"; (;;) { infile.read((char*)&s, sizeof s); if (infile.eof()) break; current = s.id; if (current != request) outfile.write((char*)&s, sizeof s); } infile.close(); outfile.close(); infile.open("c:/temp.dat)",ios::in|ios::binary); outfile.open("c:/solident.dat)", ios::out|ios::binary|ios::trunc); (;;) { infile.read((char*)&s, sizeof (s)); if (infile.eof()) break; outfile.write((char*)&s, sizeof (s)); } infile.close(); outfile.close(); } void srcrec(rec&job,fstream &infile,char fname[]) { cout << "allowing user open specific record.\n"; infile.open(fname, ios::in | ios::binary) ; if(infile.fail()) { cout << "could not access file.\n"; } else { int position; //user's position //gets user's position cout << "please record read: "; cin >> position; //ignore luki cin.ignore(numeric_limits<streamsize>::max(), '\n'); //find specific record, read it, , display infile.seekp((position - 1)*sizeof(job)); infile.read((char*) &job, sizeof(job)); cout<<"|===============================================|"<<endl; cout<<"| id | first | last |"<<endl; cout<<"|===============================================|"<<endl; cout<<left<<"| "<<setw(9)<<job.id<<"| "; cout<<left<<setw(14)<<job.fname<<"| "; cout<<left<<setw(19)<<job.lname<<"|"<<endl; } //clears , closes file infile.clear(); infile.close(); } void modrec(rec &j,fstream &outfile, char fname[]) { int position, //user's position cntr = 0; //marks current record within file cout << "modifying record.\n"; //open 2 files outfile.open(fname, ios::out | ios::in | ios::binary); //get user's desired position cout << "please enter record modify: "; cin >> position; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << endl; if(outfile.fail()) { cout << "file not read.\n"; } else { //find desired record outfile.seekp((position - 1)*(sizeof(j))); //get user's modification cout<<"please enter id number :"; cin>>j.id; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout<<"\nplease enter first name: "; cin.getline(j.fname,size); cout<<"\nplease enter lastname: "; cin.getline(j.lname,size); //write on desired record outfile.write((char*) &j, sizeof(j)); } //closes files outfile.close(); } void sort() { int structuresize, idx1, idx2; file * binaryfile; binaryfile = fopen("c:/solident.dat","rb+"); rec index, indextemp; structuresize = sizeof(index); fseek(binaryfile, 0, seek_end); int filesize = ftell(binaryfile); rewind(binaryfile); (idx1 = 0; idx1 < filesize; idx1 += structuresize) { (idx2 = 0; idx2 < filesize - structuresize; idx2 += structuresize) { fread(&index, structuresize, 1, binaryfile); fread(&indextemp, structuresize, 1, binaryfile); if (index.id > indextemp.id) { fseek(binaryfile, -(structuresize * 2), seek_cur); fwrite(&indextemp, structuresize, 1, binaryfile); fwrite(&index, structuresize, 1, binaryfile); fseek(binaryfile, -structuresize, seek_cur); } else { fseek(binaryfile, -structuresize, seek_cur); } } rewind(binaryfile); } fclose(binaryfile); }
i don't know, there seem few issues code. here's 1 of them though
int request=1,current; ... while (!infile.eof()) { s.id=current; if (current!=request)
at no point in code give current
value, still using variable. uninitialised variable 1 reason code isn't working.
i wonder if meant write
current=s.id;
instead? make little more sense.
try reading file loop this
infile.open("c:/solident.dat",ios::in|ios::out|ios::binary); if (!infile.is_open()) cout << "infile not open\n"; outfile.open("c:/temp.dat",ios::out|ios::binary|ios::trunc); if (!outfile.is_open()) cout << "outfile not open\n"; (;;) { infile.read((char*)&s, sizeof s); if (infile.eof()) break; current = s.id; if (current != request) outfile.write((char*)&s, sizeof s); } infile.close(); outfile.close();
i wouldn't use fail()
if you. think it's extremely unlikely you'll detect genuine read or write failure it. it's more end of file error, or failure open file.
Comments
Post a Comment