I'm using Linux ubuntu trying to create a file. But this way doesn't work here. How to do it right? or configure
1 2 3 4 5 6 7 8 9 10 11 12
#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;
}
I am not familiar with ubuntu and it has been awhile since I used some type of Unix.
It has been my experience that input and output file tend to be in that same directory as the ".cpp" file although there are some IDEs that will want th input and output files n the same directory as the ".exe" program.
For now I see nothing wrong with your code. Except that it could use some blank lines. As an output stream if the file does not exist it will be created before writing to the file. Not really a big problem.
If you are not sure where the file can be found try changing the file name to "Find Me.txt" and then do a search for that name and see where it ended up.
I ran your program on a Windows system using VS2017 and the program compiled and ran fine creating the file and writing to it. So I know that the code works.