Opening files with variable name?

This is a discussion on Opening files with variable name? within the C++ Programming forums, part of the General Programming Boards category; We're learning file I/O. The code below works, but the file name to open, testFile.txt, is hard coded into the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Opening files with variable name?

    We're learning file I/O. The code below works, but the file name to open, testFile.txt, is hard coded into the program. How can the filename be input by the user? I'm not using argv stuff on the command line because the compiler we're using, MSVC 6.0 Intro edition, doesn't support release versions, only debug.
    I've tried cin with strings and char * 's, but always get a compile error. Any ideas? Thanks.
    Code:
    	
       fileIn1.open("testFile.txt");

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Inputting strings with cin is the way to go.

    std::string blah;
    cin >> blah;
    std::ifstream fileIn1(blah.c_str());
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Hadn't tried that, never used c_str(). I thought that was for converting C++ strings to C strings. I'll give it a try, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening files.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-25-2008, 01:05 PM
  2. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 02:23 PM
  3. opening php files in IE6
    By Micko in forum Tech Board
    Replies: 7
    Last Post: 03-06-2006, 12:16 PM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM

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