简体   繁体   中英

c++ how to read a txt files from getline

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 file

root:password:2

How to read the password category?

Once you read the line , with "find" and "substr" , you can get the password details as,

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM