Link to home
Start Free TrialLog in
Avatar of kaio123
kaio123

asked on

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";
}


ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaio123
kaio123

ASKER

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 \.
Unix filenames always use /.  Windows uses \, but because that's a special character, you have to escape it.  Hence, c:\ becomes "c:\\".
brian
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
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