Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

how do i open and also write to a different directory?

Trying to open a a new file in binary mode in plain C++ on unix and then putting stuff and closing it. I would like to specify a path to open and store the filename . How do i do it? Here's the sample code. I also have to rename it after closing it.


const char *output_file ;

void  create_filename(int &Count, string &outFilename, struct tm *Billing_Time);


create_filename(file_seq, tempFilename, &billing_time);

output_file = tempFilename.c_str();

   ofstream myOutputFile(output_file, ios::binary);
      if ( myOutputFile.fail() ) {
         #ifdef DEBUG
          cerr << "Could not open file for output: " <<              output_file << endl ;
          cerr << "Aborting ." << endl ;
         #endif
         exit(0) ;
      }
//do stuff
.
.
.
myOutputFile.close();
string oldFile = tempFilename;
string renamedFile = tempFilename.replace(2,1,1,'U');

if(rename(oldFile.c_str(), renamedFile.c_str())!= 0) {
 
  cout<<"couldnt rename file \n";
}


0
kaio123
Asked:
kaio123
1 Solution
 
SalteCommented:
just specify path in the filename.

ofstream file("/foo/bar/baz.bin", ios::binary);

// write the file as usual.


will write to file baz.bin in that directory specified, if you specify relative path the current directory is what counts, i.e. the process' current directory.

"foo/bar/baz.bin" will refer to file relative to current directory, just as in the shell.

Be aware that ofstream will create the file if it doesn't exist but it will NOT create the directories if they do not exist.

If you want to create the directories also if they do not exist then you must do special testing and creation of those before you open the file. Use mkdir() to create a directory before you open the file.

If you don't want to do that, be ready to handle the error that 'directory not found' type error from UNIX (ENOENT).

Alf
0
 
kaio123Author Commented:
the problem is that my filename is generated depending on time so its always a new one and hence i have a string to store the filename. Does / work in the filename or should it be \.
0
 
bkrahmerCommented:
Unix filenames always use /.  Windows uses \, but because that's a special character, you have to escape it.  Hence, c:\ becomes "c:\\".
brian
0
 
udilCommented:
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.

In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0
 
udilCommented:
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
Unless there is objection or further activity,  I will suggest to

    "Answered by: Salte"

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now