ifstream error

This is a discussion on ifstream error within the C++ Programming forums, part of the General Programming Boards category; I want the following function to open a file which the name of the file is stored in file_name. However, ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    ifstream error

    I want the following function to open a file which the name of the file is stored in file_name. However, when I tried to compile it, I received no matching function for call to 'std::basic_ifstream...... error. When I replaced the file_name with actual name of the file like "data.txt", it compiled without error. Could someone please help me out? I need the users to be able to type in the file name. Thanks in advance.

    Code:
    void retrieve_file(string file_name)
          {
                string line;
                unsigned int input_index;
                string input_entry;
                
                
                ifstream input_file (file_name);
                if(input_file.is_open())
                {
                   """ """ """
                }
           }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,252
    ifstream can only take a C-style string as a filename. You should use:

    Code:
    ifstream input_file (file_name.c_str());

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Thankfully a string based constructor has been added to the C++0x draft so this won't be an issue in the future.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> Thankfully a string based constructor has been added to the C++0x draft so this won't be an issue in the future.

    Woo! That's something that's been bugging me for ages! that .c_str() ... where is this draft, Daved?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Here's the post from CornedBee where I got the link. I just downloaded it from there:

    Nested classes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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