So, this way, when I execute the program the file has to be example.txt
But what I want to see if it's possible to be done is write the name of that file inside the program( the console).
Is this possible?
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <fstream>
usingnamespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Well, one of the first thing people learn is often to use std::cin. Do you know how to use it, and do you know how to read a string (i.e. a piece of text) from the user. If you don't know this stuff I can only recommend you to read about it. It's probably at the start of every C++ book for beginners, or you could read about it in the tutorials on this site: http://www.cplusplus.com/doc/tutorial/basic_io/ (same link as given by keskiverto)
As you can see this will create a file called information.txt
What I'm asking you, is it possible to make it inside the program to name that file?
Like:
What would you call this file to be called?
And I type in: information2
and the file saves as information2.txt
I'm trying that way but I keep getting an error, I'm not doing something right, can you help me out and change it in the above example so I can see where I'm doing the mistake?
My guess is you are getting an error because you are using something before C++11 and you can't give fstream a string. Otherwise you already seem to understand how to do it all?