Hello Everyone. i need to create a txt file. the code below is the way i save my list but this wont be in txt file. the thing is, i want it to be save in text file along with saving it to dat file. how do i do it? Thank you
Make it a function that takes in the filename as a string, and then call it twice with both filenames.
1 2 3 4 5 6 7 8 9
void save_item(const std::string& filename) // change to const char* if not using C++11
{
fI.open(filename, ios::out|ios::app);
// ...
}
save_item("list.dat");
save_item("list.txt");
I also suggest making your file stream (fI) be a local variable instead of a global/class variable.
PS: Your function name is misleading. Usually, you should follow the "Do One Thing" or "Single Responsibility" rule. If your function is called "save_X", you shouldn't also be asking for user input in that function. You should only be doing saving.