简体   繁体   English

c ++ fstream关于在.txt文件的某些行上读取文件

[英]c++ fstream regarding reading file on certain lines of .txt files

So I am very new to c++ and on an assignment I have for school I am making a program that uses fstream to put in userIds and passwords into a text file and I am not sure why this will not work when trying to confirm that the username and pass are correct所以我对 C++ 很陌生,在我为学校做的一项作业中,我正在制作一个程序,该程序使用 fstream 将用户 ID 和密码放入文本文件中,但我不确定为什么在尝试确认用户名时这不起作用和通过是正确的

int login()
{
    fstream accountFile2;
    bool confirm;
    string userID;
    string accountPass;
    string IDConfirm;
    string PassConfirm;
    accountFile2.open("accountfile.txt",ios::in | ios::out);

    cout<<"Login!\n Enter your User ID: "<<endl;
    cin>>userID;
    cout<<"Please enter your account's password\n";
    cin>>accountPass;

    accountFile>>IDConfirm;
    accountFile>>PassConfirm;
    accountFile2.close();

    if((userID == IDConfirm) && (accountPass == PassConfirm))
    {
        confirm = 1;
        cout<<"success";
    }
    else
    {
        confirm =0;
        cout<<"invalid id/pass";
    }

    return confirm;
}

Assuming accountFile and accountFile2 is a typo, it works for me ( and the compiler ).假设accountFileaccountFile2是一个错字,它适用于我(和编译器)。

Double check that the file exists and contains right data.仔细检查文件是否存在并包含正确的数据。 Also beware, that std::cin >> variable for variable of type std::string will read only one word (will stop reading when it finds whitespace).还要注意, std::string类型变量的std::cin >> variable将只读取一个单词(当它找到空格时将停止读取)。 If you want to read whole line, use std::getline(std::cin, variable) instead.如果要读取整行,请改用std::getline(std::cin, variable)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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