C++ . I have this code, where it creates new account and save it as binary file.
ID: 3856613 • Letter: C
Question
C++ . I have this code, where it creates new account and save it as binary file. However, I want want this file to be regular text file. How would i modify the code?
if (x == 1){
system("cls");
Student newStud; //student class object
// Creating new account
try{
ofstream outFile;
outFile.open("student.txt", ios::binary | ios::app);
newStud.create_account();
outFile.write((char *)&newStud, sizeof(Student));
outFile.close();
}
catch (std::exception const& e){
cout << "There was an error: " << e.what() << endl;
}
}
// this is the function to create account
void Student::create_account(){
cout << " Enter account no. : ";
cin >> accountNo;
cout << " Enter the name of account holder : ";
cin >> Fname >> ch >> lName;
// ( C / S) refers to Current / Savings
cout << " Enter the type of account (C/S) : ";
cin >> AccountType;
// Coverting lower case alphabet to upper case alphabet
AccountType = toupper(AccountType);
cout << " Enter the initial amount (>=500 for savings and >=1000 for current) : ";
cin >> balance;
cout << " :: Account created successfully :: ";
}
Explanation / Answer
This should not be a problem.Here your are not creating a binary file.To create a binary file you should explicitly name the file as "student.bin" . As you have provided the name "student.txt" it should create a text file.
Please note that ios:: binary makes sure the data is read or written without translating new line characters to and from on the fly. In other words, exactly what you give the stream is exactly what's written.It does not mean that the output file is in binary.
If you are still having issues with reading the file again then you should check the instream code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.