Ofstream Question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Ofstream Question

 
0
  #1
Apr 24th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Ofstream Question

 
0
  #2
Apr 24th, 2009
  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& .
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 328
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 59
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Ofstream Question

 
0
  #3
Apr 24th, 2009
Originally Posted by Dannyo329 View 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:
  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.
  1. ofile.open(file.c_str());

Hope this is of some help,
Jas.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: Ofstream Question

 
0
  #4
Apr 25th, 2009
Thanks guys, it works great now.
C programmers will get smashed by C++ programmers.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC