in the program i am creating a file with specific path.. file name is to be entered by user and then we have to create a file but the code is giving the error written after the code.

void f_Createfile()
{
    string dirpath;  // stores final name for file with path to stored 
    string file_name; // to enter the name of file enter by user

    cout<<"please enter the file name\n";
    cin>>file_name;

    dirpath.append("d:\\");      // appending the string by necessary terms
    dirpath.append(file_name);
    dirpath.append(".txt");

    ifstream infile;
    infile.open(dirpath,ios::in); // opening the file with dirpath string.. this line is showing error

    if(infile)
    {
        cout<<"\nfile already exist\n";
        infile.close();
        f_menu();
    }
    else
    {
        cout<<" creating file...\n";
        ofstream onfile;
        onfile.open(dirpath,ios::app);
        cout<<"\nfile is created:\n";
        onfile.close();
    }
}


Error   3   error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'   c:\Documents and Settings\malaya\My Documents\Visual Studio 2008\Projects\assignment2\assignment2\assignment2.cpp   290

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use code tags: [code] //code goes here [/code]

Change dirpath to a std::wstring and in the open operations use dirpath.c_str() instead of dirpath, since you need a C style string (null terminated) in that particular function. Using the c_str() method on the wstring gives you the const wchar_t* I've never used wstring, but give that a shot. I'm not certain if you need an additional header, but since you are using these wide character methods in your code, hopefully you have some idea of that. You definitely need the c_str() one way or the other.

Edited 6 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.