-2

After executing the code, i couldn't find the newly created file in my desktop. Could anyone tell where a created file will be saved in?

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

int main() 
{
     ofstream testfile("testing.txt");
     if (testfile.is_open())
     {
        testfile << "This is a line.\n";
        testfile << "This the second line.\n";
        testfile.close();
     }
     else cout << "Unable to open file";
     return 0;
}

1 Answers1

1

"testing.txt" is a "relative" path (as opposed to a fully qualified path such as "C:\testing.txt"). Relative paths are always relative to the current working directory, which is usually where your executable is launched from. Though certain IDE's like Visual Studio sometimes set the CWD to a different folder in your project directory by default.

Edit: The reason Visual Studio can be different is that it technically launches the executable from a different directory. What really matters is where the executable is launched from.... which is usually where it is located, but not always.

RyanP
  • 1,898
  • 15
  • 20