ofstream - Save to File

M

magix

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::eek:fstream 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.
 
L

lw1a2

magix said:
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::eek:fstream 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::eek:fstream SaveFile(stream.str().c_str());

The "C:\Output" must already exist。
 
S

Salt_Peter

magix said:
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::eek:fstream 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::eek:fstream 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top