943,812 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 510
  • C++ RSS
Apr 24th, 2009
0

Ofstream Question

Expand Post »
Hi, I have a question with ofstream, is there anyway to ask the user for a file name, then create the file, and write to it?
Like:
C++ Syntax (Toggle Plain Text)
  1. string file;
  2. cout << "Enter filename:";
  3. getline(cin, file);
  4. ofstream ofile;
  5. ofile.open(file);
  6. ofile << (stuff....);
  7. ofile.close;
...except that doesn't work properly.
Thanks for any help.
Similar Threads
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Apr 24th, 2009
0

Re: Ofstream Question

C++ Syntax (Toggle Plain Text)
  1. ofile.open(file.c_str());
  2. ...
  3. ofile.close(); // function call!
In the current C++ Standard fstream::open member function wants const char* argument only, not const std::string& .
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 24th, 2009
0

Re: Ofstream Question

Click to Expand / Collapse  Quote originally posted by Dannyo329 ...
Hi, I have a question with ofstream, is there anyway to ask the user for a file name, then create the file, and write to it?
Like:
C++ Syntax (Toggle Plain Text)
  1. string file;
  2. cout << "Enter filename:";
  3. getline(cin, file);
  4. ofstream ofile;
  5. ofile.open(file);
  6. ofile << (stuff....);
  7. ofile.close;
...except that doesn't work properly.
Thanks for any help.
Assuming you're using a std::string to hold your filename, I think you just need to convert it to a c style string in your call to ofile.open....
i.e.
C++ Syntax (Toggle Plain Text)
  1. ofile.open(file.c_str());

Hope this is of some help,
Jas.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Apr 25th, 2009
0

Re: Ofstream Question

Thanks guys, it works great now.
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: terminating a process on vista
Next Thread in C++ Forum Timeline: a loop still iterating when its condition is broken





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC