Create a file at a given path using C++ in Linux -


i want create file @ given path relative current directory. following code behaves erratically. times see file created , times not. may because of change in current directory. here's code.

//for appending timestamp timeval ts; gettimeofday(&ts,null); std::string timestamp = boost::lexical_cast<std::string>(ts.tv_sec); //./folder/inner_folder existing directory std::string filename = "./folder/inner_folder/abc_"+timestamp+ ".csv"; std::ofstream output_file(filename); output_file << "abc,efg"; output_file.close(); 

now, problem file created in cases. when have command line argument input file current directory, works fine.

./program input_file 

if have this, not work

./program ./folder1/input_file 

i tried giving full path argument ofstream, still don't see files created.

what correct way this? thanks

ofstream not create missing directories in file path, must ensure directories exist , if not create them using os specific api or boost's file system library.

always check result of io operations, , query system error code determine reason failures:

if (output_ file.is_open()) {     if (!(output_file << "abc,efg"))     {         // report error.     } } else {     const int last_error = errno;     std::cerr << "failed open "               << filename               << ": "               << strerror(last_error)               << std::endl; } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -