CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   OFstream doesnbt create directories or throw errors (https://www.cfd-online.com/Forums/openfoam/61009-ofstream-doesnbt-create-directories-throw-errors.html)

brooksmoses January 26, 2006 18:53

In the code that I'm currently
 
In the code that I'm currently writing, I've got a couple of lines approximately like the following:

fileName myFile = mesh.time().rootPath() / "output" / "datafile.dat";
OFstream myStream(myFile);
myFile << "Test!" << endl;

If the "output" directory exists, this works perfectly; the file gets created and written to just as I expect.

However, if the "output" directory does not exist, then the code silently ignores my requests -- it does not create an "output" directory, nor does it throw an error about the absence of the directory or give any indication that it isn't actually writing anything.

Is there any way to tell OFstream to create the directory? Or at least to throw an error if the directory doesn't exist? If not, what's the best way to have my code create the directory?

Thanks!

niklas January 27, 2006 02:28

just add mkDir before writing
 
just add mkDir before writing

fileName myDir = mesh.time().rootPath()/"output";
mkDir(myDir);
fileName myFile = myDir/"datafile.dat";
OFstream myStream(myFile);

N

brooksmoses January 27, 2006 21:18

Thanks much! Thats just what
 
Thanks much! Thats just what I needed.

eugene January 30, 2006 06:28

For future reference, take a l
 
For future reference, take a look at OSspecific.H and/or Unix.C for the available system commands.


All times are GMT -4. The time now is 17:28.