C ++如何从GetLine读取txt文件

时间:2018-08-16 05:30:44

标签: c++

ofstream shadow,salt;
ifstream shadowread,saltread;

login:
cout << "Enter your login id" << endl;
cin >> user;
shadowread.open("salt.txt");

while(!shadowread.eof())
{
    getline(shadowread, usercmp, ":");
    shadowread.close();
    if (usercmp == user)
    {
        int captcha = rand() % 10;
        int read;
        cout << "Enter the captcha" << captcha << endl;
        cin >> read ;
        if (captcha == read)
        {
            goto enterpw;
        }
        else
            goto login;
    }
}

txt文件

root:password:2

如何阅读密码类别?

1 个答案:

答案 0 :(得分:0)

一旦您读完该行,使用“ find”和“ substr”,您将获得密码详细信息,

    int idx = 0, cnt=0;
    int pos = usercmp.find(":");
    while(pos != string::npos) {
        cnt++;
        auto val = usercmp.substr(idx,pos-idx);
        if(cnt == 3) cout << "val => " << val << endl;
        idx = pos+1;
        pos = usercmp.find(":",idx);
    }