File input - with vars

This is a discussion on File input - with vars within the C++ Programming forums, part of the General Programming Boards category; Hello....I have a quick question: How do you open a file for input, using a var. eg. I store "test.map" ...

  1. #1
    SublicK
    Guest

    Exclamation File input - with vars

    Hello....I have a quick question:

    How do you open a file for input, using a var.
    eg. I store "test.map" in the var MapFile.
    then I use:

    ifstream File1;
    File1.open (MapFile);

    Why doesn't this work?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Is test.map in your local directory? What variable type is MapFile ?
    zen

  3. #3
    Unregistered
    Guest

    hmm

    Yes the test.map is in the same dir as the project file, and the MapFile is a string...

    When I use

    ifstream File1;
    File1.open ("test.map");

    instead of

    ifstream File1;
    File1.open (MapFile);

    it works fine...

    Also the file is opened in a seperate function:
    from the main() i call

    LoadMap("test.map");

    wich uses the function:

    void LoadMap(string MapFile)
    {
    ifstream File1;
    File1.open (MapFile);
    ...
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    It is a string, as defined in cstring.h?

    With this data type, iut will NOT automatically convert to a const char * which is what you need. To get that, you'd need to use

    File1.open(MapFile.c_str());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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