0

I got a problem with my code, I basically want to write files to a path but it doesn't seems to work. Here's the code:

void WriteOnFile(const char *path)
{
    ofstream filetolog;
    filetolog.open(path);
    if (filetolog)
        filetolog<< "--[[ Test ]]\n";
}

and then I call WriteOnFile like that:

WriteOnFile("C:/Logs/lua/testing/filename.lua");

but it doesn't work. I also tried without if (filetolog) but it's the same. Could anyone help me please?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    check if(filetolog.is_open()) is true. – smaryus Jul 01 '14 at 19:56
  • 1
    Are you asking for all the circumstances that would cause `ofstream::open` to fail? I'm guessing that "it doesn't work" means that your `if` evaluates to false. Is that what you're saying? – Drew Dormann Jul 01 '14 at 19:56
  • Here's a similar topic with a working solution, hope this helps! http://stackoverflow.com/questions/9739948/write-a-file-in-a-specific-path-in-c – abdul Jul 01 '14 at 22:15

1 Answers1

0

Your code won't create the file if one of the folders "c:/Logs", "c:/Logs/lua", "c:/Logs/lua/testing" does not exist.

Oleg
  • 1,037
  • 7
  • 13
  • ok, but is there a way to create all of throses folders with one function? (because WriteOnFile gets called with folder names I don't know) – user3795235 Jul 01 '14 at 20:03
  • There is no portable standard way of creating folders with C++. Perhaps, the easiest non-portable, Windows way is system("mkdir " + strPath). Note, however, that here the strPath must use backslashes instead of slashes to separate folder names. For more complicated Windows way, please see http://stackoverflow.com/questions/1530760/how-do-i-recursively-create-a-folder-in-win32 – Oleg Jul 01 '14 at 20:10