Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ help, How do i modify the code bellow so that instead of binary it read and

ID: 3856586 • Letter: C

Question

C++ help, How do i modify the code bellow so that instead of binary it read and write as regual text file?

try{

ifstream inFile;

// vector to store name and sort on the basis of name

vector<pair<string, pair<char, pair<int, int> > > > v;

v.clear();

inFile.open("student.txt", ios::binary);

cout<<" ACCOUNT HOLDER LIST [Student] ";

cout<<"==================================================== ";

cout<<"A/c no. NAME Type Balance ";

cout<<"==================================================== ";

while ( inFile.read((char *) &oldStud, sizeof(Student)) ){

v.push_back(make_pair(oldStud.getName(), make_pair(oldStud.retType(), make_pair(oldStud.retAc(), oldStud.retDep()))));

}

// function to sort vector

sort(v.begin(), v.end());

// Iterating the vector and showing all the elements of vector in lexicographical order

for ( int i = 0 ; i < v.size() ; i ++ ){

cout << v[i].second.second.first << setw(10) << " " << v[i].first << setw(10) << " " << v[i].second.first << setw(6) << v[i].second.second.second << endl;

}

inFile.close();

}

catch(std::exception const& e){

cout << "There was an error: " << e.what() << endl;

}

Explanation / Answer

In text file mode, file mode ios::binary is not included while opening a file.

try{

ifstream inFile;

// vector to store name and sort on the basis of name

vector<pair<string, pair<char, pair<int, int> > > > v;

v.clear();

inFile.open("student.txt");

cout<<" ACCOUNT HOLDER LIST [Student] ";

cout<<"==================================================== ";

cout<<"A/c no. NAME Type Balance ";

cout<<"==================================================== ";

while ( !inFile.eof() ){

v.push_back(make_pair(oldStud.getName(), make_pair(oldStud.retType(), make_pair(oldStud.retAc(), oldStud.retDep()))));

}

// function to sort vector

sort(v.begin(), v.end());

// Iterating the vector and showing all the elements of vector in lexicographical order

for ( int i = 0 ; i < v.size() ; i ++ ){

cout << v[i].second.second.first << setw(10) << " " << v[i].first << setw(10) << " " << v[i].second.first << setw(6) << v[i].second.second.second << endl;

}

inFile.close();

}

catch(std::exception const& e){

cout << "There was an error: " << e.what() << endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote