Printing the file name of an ofstream

Hi all,
first time posting here so I hope I am doing it correctly.

I tried to search for a solution on the web, but with little success.

I am looking for a way to print the filename I used in an ofstream (to tell the end user that the data has been saved to [filename]).

The first workaround that came to my mind is to hardcode the name in the cout (last line before return 0; - the additional << is just to better visualize where I am having the problem):
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
using namespace std;

int main() {
	ofstream outputFile("output.txt");
	outputFile << "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt";
	outputFile << "  ut labore et dolore magna aliquyam erat, sed diam voluptua.";

	cout << "Some text has been saved to " << "output.txt" << "." << endl;
	return 0;
}


But the problem is that my actual code is much longer than that and if I later change the filename in the ofstream declaration, I may easily forget to manually update the cout.

The only solution I found around the web is to create a string variable with the filename and use it in the ofstream declaration (and in the final cout in my case). It would work and considering today RAM size an additional variable wouldn't be much of a problem, but since I am learning C++ it would be a good case to learn to not waste resources if an alternative is available.

Thanks in advance.
> create a string variable with the filename and use it in the ofstream declaration (and in the final cout in my case).

Yes. That would be the simplest solution.
Hi JLBorges,
thank you for your answer.

I'll use a separate string variable then. :)
Topic archived. No new replies allowed.