Help with File I/O

This is a discussion on Help with File I/O within the C++ Programming forums, part of the General Programming Boards category; Is it possible to some how take a file name from cin and use that to open the file? EXAMPLE: ...

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

    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?

    EXAMPLE:
    Code:
    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
    Location
    Singapore
    Posts
    23,628
    Like this:
    Code:
    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
    Posts
    19
    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, 01:43 PM
  3. Replies: 2
    Last Post: 03-04-2010, 03:19 AM
  4. Replies: 3
    Last Post: 11-21-2006, 06:26 PM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21