473,698 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ofstream - Save to File

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::stringstre am 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
3 9936

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::stringstre am 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::stringstre am stream;
stream << "C:\\Output\\Ou tput.txt" << std::flush;
std::ofstream SaveFile(stream .str().c_str()) ;

The "C:\Output" must already exist。

Oct 1 '06 #2

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::stringstre am 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
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::stringstre am stream;
stream << "Output.txt " << std::flush;
std::ofstream SaveFile(stream .str().c_str()) ;
Example:
C:\Programmin g\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::stringstre am stream;
stream << "C:\\Output\\Ou tput.txt" << std::flush;
std::ofstream SaveFile(stream .str().c_str()) ;

The "C:\Output" must already exist。

Looking into boost::filesyst em 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2895
by: juli jul | last post by:
Hello,how can I override(save) file in c#? (I am working with XmlDocumnet) Thanks! *** Sent via Developersdex http://www.developersdex.com ***
9
3018
by: Ivan Demkovitch | last post by:
Hi! I would like to know if I can save File on Server using server-side code? For example, I like to create thumbnail images and populate specific directory. Do I need specific permissions to do this? (I use public host) Thanks!
1
441
by: Kapil | last post by:
Hi All can u tell me how to save dynamically generated file in asp.net. like save file dialog in asp.net. kapil
6
2499
by: Mike | last post by:
can i open the save file dialog box from a asp.net web page? thx
4
6257
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
3
3510
by: mse07 | last post by:
hi for every one i want to save text file in specified bath without show the save window i write code that save file but that code show save window : CommonDialog1.FileName = "invoices.txt" CommonDialog1.ShowSave Call FileCopy(App.Path & "\invoices.txt", CommonDialog1.FileName)
2
7766
by: AccessHunter | last post by:
Hi, In my VBA code I am saving an excel spreadsheet with data from a table. I would like to prompt the Save File or Save As Dialog box with a default File Name, after opening the spreadsheet. Please help me with the code. Thanks
3
1357
by: vedika | last post by:
hi, I created one application in which there is export functionality. I am using sqlCE as database . When user click on "export" a save file dialog appeare and file saved in html format from database. but after that if i minimise application to systray and again open it then no any data from database will displayed. If i save file in application startup path then on data from database is displaying. Thanks Vedika
2
2522
by: simonyong | last post by:
Hello, anyone I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop what I had done currently is uploading a file to database but totally no idea about how to "download" from database the following is my codes to upload file to database: If Not (myFile.PostedFile Is Nothing) Then OpenConn()
1
1849
by: mihir0288 | last post by:
I want to save HTML file that i open in web browser on my hard disk without using save file dialog box. I want to save it as html page through only code and without user interactions.
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9023
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.