Getting filepath from string

This is a discussion on Getting filepath from string within the C++ Programming forums, part of the General Programming Boards category; I want the user to input a file path, set it to a string and make an ifstream for the ...

  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
    Katy, Texas
    Posts
    2,309
    You can't pass a C++ string. Pass a C string.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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, 03:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05: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, 07:49 PM

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