Where should a file be located after writing on it?

Hello,

I wrote to a file called "test.txt" and I tried to look for it and could not find it anywhere using search engines. I thought the file does not exist, but I wrote another code that reads from the same file "test.txt", it reads it successfully. Any idea where the file should be located at?

Writing code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
using namespace std;

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


Reading code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("test.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
In the same location as your binary..
if you use a tool like visual studio is creates it in another location and adds debug data... so the file would be over there...
if you run the binary directly it would be created in the same folder.
That's what I thought! but I could not find it!
If your not running it from the command line you would know what directory your in but if your running from your compiler or something who knows.

Assuming your using windows, Try this;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("test.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
    system ("dir test.txt");
//    system ("type test.txt");
     system("pause");
  }
  else
  {
  cout << "Unable to open file";
  system ("dir test.txt");
  }
  return 0;
}
Topic archived. No new replies allowed.