Thread: Help with File I/O

  1. #1
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011

    Red face Help with File I/O

    Is it possible to some how take a file name from cin and use that to open the file?

    int main() 
      string filename;
      cout << "Please enter the file name to open: ";   
      cin >> filename;   
      ofstream a_file ( **HOW TO CALL IT HERE?** );  
    Sorry if this question is silly, I am still learning!

    Thank you much

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Like this:
    ofstream a_file(filename.c_str());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User MoonMan's Avatar
    Join Date
    Nov 2011
    Thank you very much!

    best regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  3. Replies: 2
    Last Post: 03-04-2010, 04:19 AM
  4. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM