该函数应该读取文件的每一行并将其与用户输入的字符串进行比较并检查它们是否匹配。它基本上可以防止文件中的重复信息。无论如何,程序没有进入我的“if (loginsFile.is_open())”语句,我不明白为什么。
fstream loginsFile;
loginsFile.open("C:/logins.txt", ios::in | ios::out | ios::trunc | ios :: app | ios:: ate);
string username;
string password;
string info;
bool exists = true;
CheckingAccount cA;
SavingsAccount sA;
do {
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
cout << endl;
info = username + " " + password;
if (loginsFile.is_open()){
while (loginsFile.good()){
string line;
getline(loginsFile, line);
cout << "line is " << line.substr(line.find_last_of(" ")) << "\n" << "info is " << line.substr(line.find_last_of(" "));
if (line.substr(line.find_last_of(" ")) == info.substr(0, info.find_last_of(" "))){
exists = false;
cout << "Username already exists!" << endl << "Program is not case sensitive!";
} //end if
} //end while
} //end if
} while (exists == true); //end do while
loginsFile << info << endl;
loginsFile.close();
logins[info] = make_pair(cA, sA);
cout << info.substr(0, info.find(' ')) << " Has Been Successfully Registered!" << "\n" << "\n";
return logins;