c++ - Delete specific line from file -


i'm trying delete specific line id file in c++ , here code:

void deleterow() {     ifstream indb("files/students.dat", ios::in);     if(!indb)     {         cerr << "file no opened!\n";     }      cout << countrowsofdb() << " records." << endl;      student *studentsarray[countrowsofdb()];      int n = 0;      while(indb >> id >> name >> grade >> points >> type)     {         studentsarray[n] = new student(id, name, grade, points, type);         n++;     }      indb.close();      for(int = 0; < countrowsofdb(); i++)     {         cout << studentsarray[i]->id << " " << studentsarray[i]->name << " " << studentsarray[i]->grade << " "              << studentsarray[i]->points << " " << studentsarray[i]->type << "\n";     }      cout << "\nwhich 1 delete? enter id: ";      string term;     cin >> term;      ofstream outdb("files/students.dat", ios::out);     if(!outdb)     {         cerr << "file no opened!\n";     }       for(int = 0; < countrowsofdb(); i++)     {         if(studentsarray[i]->id != term)         {                 outdb << studentsarray[i]->id << " " << studentsarray[i]->name << " " << studentsarray[i]->grade << " "                       << studentsarray[i]->points << " " << studentsarray[i]->type << "\n";         }     }      outdb.close();      cout << "\nobject deleted!\n"; } 

i create input file stream , rows, make array of objects , show them on screen ask 1 delete typing id, , when type id, i'm trying put these elements of array without element same id, don't works, after there nothing in file. ideas?

what's in countrowsofdb()? if opens file , counts lines in (and don't know else do), it's not going find lot in final loop, since creation of ostream same name have emptied file.

more generally, inefficient way of doing things (and fail if there error in format of file). best way handle use std::vector<student>, with:

studentvector.push_back( student( id, name, grade, points, type ) ); 

in input loop. in later loops, studentvector.size() gives number of entries, or can use iterators.

even better use std::getline on input, initialize std::istringstream parse each line. catch input format errors more reliably. like:

std::string line; int linenumber = 0; while ( std::getline( indb, line ) ) {     ++ linenumber;     std::istringstream data( line );     if ( data >> id >> name >> grade >> points >> type ) {         studentvector.push_back( student( id, name, grade, points, type ) );     } else {         std::cerr << "format error in lne " << linenumber << std::endl;     } } 

also, better idea write separate file, rename after having verified write worked, i.e.:

std::ofstream outdb( "files/students.dat.new" ); //  output... outdb.close(); if ( outdb ) {     remove( "files/students.dat" );     rename( "files/students.dat.new", "files/students.dat" ); } else {     std::cerr << "write error on output" << std::endl; } 

and of course, write error should result in return of exit_failure main. (this 1 case global variable or singleton justified—keeping track of return code.)


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -