Thread: ifstream error

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    ifstream error

    I want the user to input a file name and then the program reads from that file and does what it needs to do.

    Code:
    cin >> file;
    ifstream bookmark (file);
    Before I was just using
    Code:
    ifstream bookmark ("bookmarks.html")
    but when I try and replace the actual file with a variable it gives me the error
    Code:
    .\CBookmarks.cpp(36) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
            with
            [
                _Elem=char,
                _Traits=std::char_traits<char>
            ]
            No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't create a file from a string, because the "string" type was introduced very late in the C++ standard.

    You can do
    Code:
    ofstream bookmark(file.c_str());
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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, 04: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, 07:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM