By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,742 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

ofstream - Save to File

P: n/a
Hi,

I got following to savefile into Output.txt, but how can I specify it to be
in specific folder/directory (not at the same directory as the executables?
Currently, it will generate the output.txt under the same directory as the
executables.

std::stringstream stream;
stream << "Output.txt" << std::flush;
std::ofstream SaveFile(stream.str().c_str());
Example:
C:\Programming\test input.txt
it will generate output.txt in the same directory as where the executable
"test" located

If I want output.txt to be generated in C:\Programming\Output\ or at other
directory i.e C:\Output\, how to achieve that ?

Please help. Thanks.

Regards.
Oct 1 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

magix wrote:
Hi,

I got following to savefile into Output.txt, but how can I specify it to be
in specific folder/directory (not at the same directory as the executables?
Currently, it will generate the output.txt under the same directory as the
executables.

std::stringstream stream;
stream << "Output.txt" << std::flush;
std::ofstream SaveFile(stream.str().c_str());
Example:
C:\Programming\test input.txt
it will generate output.txt in the same directory as where the executable
"test" located

If I want output.txt to be generated in C:\Programming\Output\ or at other
directory i.e C:\Output\, how to achieve that ?

Please help. Thanks.

Regards.
std::stringstream stream;
stream << "C:\\Output\\Output.txt" << std::flush;
std::ofstream SaveFile(stream.str().c_str());

The "C:\Output" must already exist。

Oct 1 '06 #2

P: n/a

magix wrote:
Hi,

I got following to savefile into Output.txt, but how can I specify it to be
in specific folder/directory (not at the same directory as the executables?
Currently, it will generate the output.txt under the same directory as the
executables.

std::stringstream stream;
stream << "Output.txt" << std::flush;
std::ofstream SaveFile(stream.str().c_str());
Example:
C:\Programming\test input.txt
it will generate output.txt in the same directory as where the executable
"test" located

If I want output.txt to be generated in C:\Programming\Output\ or at other
directory i.e C:\Output\, how to achieve that ?

Please help. Thanks.

Regards.
Remember that '\' implies a switch and one of those switches is used to
inject a backslash (ie:'\\').
You can also try '/' instead which is not parsed as a switch. You'll
have to test that on your specific platform to find out if it works at
all.
In order to help yourself with an issue like this, a little error
checking is very handy (if not a requirement). And one typical way that
you might consider doing this is a std::exception.

Try the following on your platform and change the directory to whatever
you need.

#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <stdexcept>

class FileException : public std::exception
{
std::string s_e;
public:
FileException( std::string s ) : s_e( s ) { }
~FileException() throw() { }
const char* what() const throw() { return s_e.c_str(); }
};

int main()
{
std::string s_file("output.txt");
std::string s_target("/test/");
s_target += s_file;

try
{
std::ofstream ofs( s_target.c_str() );
if ( !ofs.is_open() )
{
throw FileException("could not open file " + s_target);
}
ofs << s_target << std::endl;
}
catch ( const std::exception& e )
{
std::cout << "Error: " << e.what() << std::endl;
}
return 0;
}

write-protect the file:
Error: could not open file /test/output.txt

hmm...
Now code a templated File class that can read() and write() whatever
you need.
File< std::string file; // a std::deque of strings?
file.read("data.txt"); // load the container with the file's contents

Oct 1 '06 #3

P: n/a
lw1a2 wrote:
magix wrote:
>>Hi,

I got following to savefile into Output.txt, but how can I specify it to be
in specific folder/directory (not at the same directory as the executables?
Currently, it will generate the output.txt under the same directory as the
executables.

std::stringstream stream;
stream << "Output.txt" << std::flush;
std::ofstream SaveFile(stream.str().c_str());
Example:
C:\Programming\test input.txt
it will generate output.txt in the same directory as where the executable
"test" located

If I want output.txt to be generated in C:\Programming\Output\ or at other
directory i.e C:\Output\, how to achieve that ?

Please help. Thanks.

Regards.


std::stringstream stream;
stream << "C:\\Output\\Output.txt" << std::flush;
std::ofstream SaveFile(stream.str().c_str());

The "C:\Output" must already exist。

Looking into boost::filesystem at http://www.boost.org/ might also be
valuable.
Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Oct 1 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.