Opening and writing to a file

Hi there, I tried to open and write to files but I can't get it to work. I tried the code below in Coderunner app and in Xcode. I was expecting text editor to open a file, write to it, and save it somewhere. Program runs but nothing happens, and if I comment out myfile.close() I don't see and open .txt file anywhere. The solution is probably very simple, thanks in advance for help.

1
2
3
4
5
6
7
8
9
10
11
  #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;
}
I was expecting text editor to open a file, write to it, and save it somewhere


actually, no text editor will open, the file will be saved in the background.

Try looking instead in the directory where your project is located, it should be somewhere there.
Yeah it is! Thanks very much. It wasn't at first because I hadn't saved the code snippet. When I saved it to Desktop and ran the .txt file appeared.
Topic archived. No new replies allowed.