Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,234 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,685 people online right now. Registration is fast and FREE... Join Now!




File Streams

 
Reply to this topicStart new topic

File Streams

TwoSpots
15 Mar, 2007 - 03:23 PM
Post #1

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 45


My Contributions
Hello all,

I am having difficulties with a problem and would appreciate any help.

The question that I am working on asks to store the results in a text file specified by the user. I have no problems outputting the results to a predefined text file (“example.txt”) however; I am not able to print the results to a text file of the user’s choice ("User_choice.txt").

i.e.
string file_name;
cout << "Please enter the filename and extension, that would you like to store your results in (i.e. test.txt): ";
getline (cin,file_name);
cout << "Thank you, your results are stored in " << file_name << "." << endl;

ofstream outfile;
outfile.open ("User_choice.txt"); // and not(“file_name”)

How do I create and open a file specified by the user so that I can store data in this file?

Thanks

TwoSpots.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: File Streams
15 Mar, 2007 - 03:52 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You can use the variable that you created, but do not use quotes around it.
CODE

outfile.open (file_name); // and not(“file_name”)

User is online!Profile CardPM
+Quote Post

TwoSpots
RE: File Streams
15 Mar, 2007 - 06:30 PM
Post #3

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 45


My Contributions
QUOTE(jayman9 @ 15 Mar, 2007 - 04:52 PM) *

You can use the variable that you created, but do not use quotes around it.
CODE

outfile.open (file_name); // and not(“file_name”)



Hello Jayman,

Thanks for the immediate response. I tried removing the quotes however, I get the following error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char>>::open(std::string&)’.

I also tried outfile.open(file_name,ios::out); but no luck. For the record I did included: #include <fstream>.

Twospots.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: File Streams
15 Mar, 2007 - 06:44 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Oops, sorry.

You need to convert the string to a c-string.
CODE

outfile.open (file_name.c_str( ));


User is online!Profile CardPM
+Quote Post

TwoSpots
RE: File Streams
15 Mar, 2007 - 06:49 PM
Post #5

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 45


My Contributions
It worked! Thanks a lot Jayman~

QUOTE(jayman9 @ 15 Mar, 2007 - 07:44 PM) *

Oops, sorry.

You need to convert the string to a c-string.
CODE

outfile.open (file_name.c_str( ));



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:54PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month