Ok i got a problem with trying to read off a file that doesnt exist yet something like this

string fname;
ifstream InObj;
 
       cout << "Enter name of file name: ";
       cin >> fname;

       InObj.open(fname);

Then i got this error:
N:\VC++\LAB01A\F06Lab1a.cpp(97) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >::open(const char *,int)' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std
::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

I try it with Char and it seem to work but when running the program it seem to skip that part... or something the program pop up with a window with option of fixing bug or closing it.

Is there anyway of reading a file as u enter?

open expects a c-style string. You can use the c_str() std::string member function:

InObj.open(fname.c_str());

Yes maybe something along the lines what Mr. GloriousEremite has given should solve your problem.

int main (void)
{

    ifstream in_file ;
   
     // start of naked block according to the principle of least
     // data exposure.
    {
        string file_name ;
        cout << "Enter the name of the file: " ;
        cin >> file_name ;
        in_file.open ( file_name.c_str ( ) ) ;
     }

    if ( in_file ) {
        cout << "\nOpening of file was successful " ;
    }
    else {
        cerr << "\nFailed to open the file " ;
        return ;
    }

    cin.get () ;
    return 0 ;
}

HOpe it helped, bye.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.