File handling String related question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SUNIL TYATA
    New Member
    • Feb 2010
    • 18

    File handling String related question

    I've declared the string realPath as
    string realPath;
    realPath = "c:/tc/matrix.txt";

    when i use realPath for the path of file to open like

    fstream infile;
    infile.open(realPath, ios::in); //This line generate error like
    ///////////////////////////////////////
    error msg:
    1>.\read_matrix _table.cpp(31) : error C2664: 'void std::basic_fstr eam<_Elem,_Trai ts>::open(const wchar_t *,std::ios_base ::openmode,int) ' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::ch ar_traits<char>
    1> ]
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    //////////////////////////////////
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    The problem is you're using a C++ string, but you actually need a C null-terminated string (don't worry, they're included in C++, C null-terminated string is just a fancy way of saying array of chars containing the characters of your string and with a null-value after those to mark the end of that string).

    It's a bit stupid that fstream, as part of the standard library, doesn't support string, but luckily there's an easy workaround: you can use the c_str()-function to get the C-style string from your C++-style string.

    Comment

    Working...