I'm looking for open the TXT file and search USERNAME and PASSWORD, but I got this message 'failed to open file'.

ifstream myfile("E:\\University\\usersinfo.txt");

//*************************************************************

cout<<"\n\n*****************Please insert user name and password to LOGIN**************"<<"\n\n";

cout<<"please insert username -->";
cin>>UserName;
cout<<"Please insert password -->";
cin>>Password;

//*******************************************************
// For reading the file
myfile.open("usersinfo.txt",ios::in | ios::out );
//*******************************************************

if(myfile.fail())
{
cout<< "failed to open file!"<<endl;
}

if (!myfile.is_open())
{
cerr << "Could not find file." << endl;
exit(1);
}

while(myfile.good())
{
getline(myfile,TempUserType);
getline(myfile,TempUserName);
getline(myfile,TempPassword);
}
if(TempUserName == UserName && TempPassword == Password)
{
system ("cls");

cout<<"\t******LOGIN SUCCESSFULL !*******"<<"\n\n";
cout<<"**********Please select one of the following******** "<<endl;
cout <<"\t **** You can select the following number ";
}

}
Last edited on
Where is the data file in relation to your current directory when you run the program?
i.e. What directory is the data file in? What is your current directory when you run the program?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.

Topic archived. No new replies allowed.