1

I want the line below to write a new file using the content given in the array but into a new folder named logs:

char log_file_name[100]; /* this array contains the name of a new file */

ofstream write_to_log;
write_to_log.open (relative path, log_file_name , fstream::app); 

How do I get it working ?

perror
  • 7,071
  • 16
  • 58
  • 85
  • You will need a system call to create a new folder. Are you using Windows, Linux, something else ? Would you be happy with a solution using boost libraries ? – Manos Nikolaidis Sep 06 '15 at 14:32
  • Windows 10 64bit pro. I forgot to mention that the folder is already in existence. I just need to know how to get the ofstream to write the file into that folder using the content given in the array as the filename –  Sep 06 '15 at 14:40
  • Since I answered before you commented ... now you also know how to create folders as a bonus ;-) – Manos Nikolaidis Sep 06 '15 at 15:02

2 Answers2

1

You can use CreateDirectory for creating folders with VC++ in Windows.

#include <windows.h>
#include <string>
#include <fstream>
using namespace std;

int main() {
   string path = "C:\\users\\folder";
   CreateDirectory(path.c_str(), NULL);

   char log_file_name[100] = "log.txt";
   path += '\\';
   path += log_file_name;
   ofstream write_to_log(path.c_str(), fstream::app);

   return 0;
}

The NULL refers to a security attributes structure that you may have to create. More details at MSDN here and in this answer.

Community
  • 1
  • 1
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • 1
    `warning: adding 'char' to a string pointer does not append to the string`. – Roddy Sep 06 '15 at 16:26
  • I also fixed the string concatenation. Note to self : check code with a compiler before posting on stackoveflow. – Manos Nikolaidis Sep 06 '15 at 16:27
  • @Roddy the log_file_name gets its content from a file generated by a batch which goes through serveral files and copies their names one by one to a file. In the end I hope to be able to write a seperat log file for each file, Actually this programm is only supposed to add content to a log file. the file is later edited by another programm. –  Sep 06 '15 at 16:34
  • @Roddy I assumed this char array is already in his code. Otherwise an std::string is recommended. – Manos Nikolaidis Sep 06 '15 at 16:36
  • I have changed the char array to a string, I also added the path like this: string patch = "\\logs"; This should work since the exe and the logs folder are in the same directory? Whats the best way to post the error messages? –  Sep 06 '15 at 16:43
  • That depends what the current folder is. You can get it from C++ like [this](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx). If `logs` is a subfolder of current folder and log.txt is in there then eventually the path for ofstream should be "logs\\log.txt" not "\\logs\\log.txt" – Manos Nikolaidis Sep 06 '15 at 18:25
0

You can save your self a lot of potential trouble and replace char log_file_name[100]; with std::string log_file_name; The benefits of string are many, the most important here are they resize and they make appending really easy. The string does everything a char array does and a whole lot of extras. In virtually all cases, you should chose a string over a char array.

string path;
string log_file_name;

With the path and the file name as strings

path += "\\" + log_file_name
ofstream write_to_log(path, fstream::app);
if (write_to_log)
{ // file is open and looks writable (have to start writing to be sure)
    // do stuff. Or not. It's a free country.
}
else
{ // file didn't open
    // Handle error
}

All done and the file, if it exists and is writable, is open and ready to go. Always check the state of a stream when you use it. SO is littered with questions from people who didn't and got confused by the result.

On older compilers you may have to change the create and open line slightly:

ofstream write_to_log(path.c_str(), fstream::app);
user4581301
  • 33,082
  • 7
  • 33
  • 54