?
Solved

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

Posted on 2003-02-19
5
Medium Priority
?
231 Views
Last Modified: 2010-04-01
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
Comment
Question by:kaio123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 7983045
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
 

Author Comment

by:kaio123
ID: 7987638
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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8031689
Unix filenames always use /.  Windows uses \, but because that's a special character, you have to escape it.  Hence, c:\ becomes "c:\\".
brian
0
 
LVL 2

Expert Comment

by:udil
ID: 8867117
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
 
LVL 2

Expert Comment

by:udil
ID: 8951110
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question