Thread: Getting filepath from string

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Getting filepath from string

    I want the user to input a file path, set it to a string and make an ifstream for the file.

    Code:
    string filepath;
        cout<<"Please enter a file path."<<endl;
        cin>>filepath;
        ifstream theFile(filepath);
    the compiler is telling me --


    13 C:\Users\Sterling\Desktop\C++ Practice\NumberAnalysis.cpp no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)'

    line 13 is ifstream theFile(filepath);

    I'm kinda used to Java and just took a Java approach to it. Is there a way to do this using strings or something else?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't pass a C++ string. Pass a C string.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    ifstream - C++ Reference

    try:
    Code:
     ifstream theFile(filepath.c_str());

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    as mentioned, the API for ifstream says this about the "filename" argument:
    Quote Originally Posted by cplusplus.com
    C-string containing the name of the file to be opened.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Awesome, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM