Fstream: using ofstream but doesn't creat a file

Hello everyone!

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>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Last edited on
Are you checking for the file in the correct area? It's going to created where ever the .exe is most likely.

You can replace "example.txt" with the actual directory you want it to be created in to check.
When a compiled it was created an .exe.
manually created a .txt file to test and nothing was inserted into the file.
What can this be?
Hello Null server,

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.

Hope that helps,

Andy
I had just compiled it.
And to create the file have to run that was my mistake.
Your text gave me that light

Thanks Andy

[Solved]
Topic archived. No new replies allowed.