0

I'm trying to create a log file using the code below, but It's not working and I'm unable to get the logs as the file is not created!

#include <iostream>
#include <fstream>
using namespace std;

ofstream myfile;

int main () {

  myfile.open ("example.txt");
  if (!myfile.is_open())
  {
      cerr << "Failed to create Log" << endl;
  }
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;

}
user9059547
  • 67
  • 1
  • 12

2 Answers2

0

For me, it is working fine. compiled with g++ on ubuntu.

If your are using linux, try locating example.txt as may be the working directory is different. In windows, you can search for the file in your computer.

Omar
  • 11
  • 3
0

I managed to get your code working, as it worked out of the box. Files created adhere to an odd structure, so you'll need to put your assembly version as a prefix either through a define or a variable.

You can find example.txt up a directory from Debug/Release, next to your .vcxproj file, and in the folder next to your .sln file.

Tinkeroonie is the name of my project, yours might be LoggerTest or something like that.

Highlighted is the folder you need.

James Whyte
  • 788
  • 3
  • 14