saving to txt file

closed account (iN8poG1T)
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

1
2
3
4
5
6
7
8
9
10
11
12
  void save_item()
{
	fI.open("list.dat",ios::out|ios::app);
    product.enter_item();
	  //to write the file
	fI.write((char*)&product,sizeof(item));
	fI.close();
	cout<< endl;
	cout<<" Item Entered"<<endl;

	getchar();
}
You want to write to two files instead of one?

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.
Last edited on
Topic archived. No new replies allowed.