0

I want to use C++ to create files which I can then export for use elsewhere. The following code seems to create files okay, in the sense that I can write data into the file and then read it again later in a C++ program. When I try to actually find created file so as to use it, however, it is nowhere to be found..

using namespace std;

int main () {

    ofstream myfile ("example3.dat");
    if (myfile.is_open()){

        myfile << 3335 << " " << 64  << " " << 43 <<  9 << 5 << 6 << 5 << 4 << 6;
        myfile.close();
    }
    else cout << "Unable to open file";

    ifstream myfile2 ("example3.dat");

    int b;
    myfile2 >> b; cout <<b;
    myfile2 >> b; cout <<b;
    myfile2.close();

    return 0;
}
jschroedl
  • 4,916
  • 3
  • 31
  • 46
  • `cd /` `find . -name example3.dat -print` – Almo Dec 17 '14 at 15:28
  • 1
    @sleeping_dragon: Current directory isn't always the directory of the exe. – RvdK Dec 17 '14 at 15:29
  • Could it be in the Debug folder? – JMc Dec 17 '14 at 15:31
  • 1
    This question appears to be off-topic because it is about finding a file on your computer's hard drive. – Lightness Races in Orbit Dec 17 '14 at 15:38
  • To clarify comment by @sleeping_dragon - the files will be in the directory from which you ran the program, since the program uses only file names, and never changes directory. That can very well be different than the location of the program itself, if you ran it as e.g. `/path/to/my/program`... – twalberg Dec 17 '14 at 15:45
  • @LightnessRacesinOrbit it's not about finding a file. It's about figuring out where a C++ program in general will put files it writes. – Almo Dec 17 '14 at 15:53
  • @twalberg thanks, I'll delete my comment since its misleading and cannot be edited! – sleeping_dragon Dec 17 '14 at 21:56

2 Answers2

4

The files will be created in the process working directory.

In case the process is being run from an IDE (e.g. Visual Studio), the working directory can be different from the executable file path. You should check the project properties to find out the actual path.

David
  • 2,663
  • 3
  • 24
  • 41
2

Certainly you can search for the file as suggested in comments above, but you might be better off specifying an absolute path in your program, so that you know where it's being written.

The formatting of the path is OS-specific, but maybe

/tmp/example.dat

C:\Windows\Temp\example.dat

for Linux and Windows respectively (but you will need to decide for yourself; these are just examples).

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • Speaking generally, hardcoding an absolute path into a computer program is a _terrible_ idea. – Lightness Races in Orbit Dec 17 '14 at 15:40
  • @LightnessRacesinOrbit it depends on the context. If you know where you want the files to go, and it's code for a very specific purpose, it's OK. The better option would be to allow the user to specify the location, but that's an extra level of complexity that OP may not need. – chiastic-security Dec 17 '14 at 15:52
  • In the general case you should take the current working directory. It's a concept that exists for a reason! That's generally _less_ complexity than anything else you could come up with, all things being equal. – Lightness Races in Orbit Dec 17 '14 at 17:06
  • @LightnessRacesinOrbit yes, I agree that's the typical case (and the right thing to do if you understand the idea of a cwd). – chiastic-security Dec 17 '14 at 17:09
  • Any programmer who doesn't understand the idea of a cwd should learn the idea of a cwd before deciding not to use it. And then they should decide to use it. :) – Lightness Races in Orbit Dec 17 '14 at 17:11