c ++:从文件/解析字符串中读取

时间:2014-12-12 06:43:02

标签: c++ parsing stringstream

我目前正在开发一个程序,程序的用户需要输入用户名和密码,程序会进行搜索并查看是否有这样的用户,如果存在,则继续进入屏幕更多选项,或重新输入用户名。

存储用户名和密码的username.txt文件包括以下数据:(第一列是用户名,第二列是密码)

john,abc
marry,cde
admin,admin
joseph,1234

我的代码如下,但它不起作用,输入用户名和密码后,程序会自动关闭。你能帮帮我吗?我将字符串解析为2时有什么问题吗?

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<math.h>
#include<stdio.h>
using namespace std;

void printoptionsadmin(){
    cout << "Please select an option:" << endl;
    cout << "1.Sell Stock\n2.Buy stock\n3.Inquiry\n4.Logout\n5.Shutdown" << endl;
}
void printoptions(){
    cout << "Please select an option:" << endl;
    cout << "1.Sell Stock\n2.Buy stock\n3.Inquiry\n4.Logout" << endl;
}

void main()
{

    cout << "Please login." << endl;

    stop:
    string usertype;//user input
    string passtype;//user input
    string line;
    string manager = "admin";
    string managerp = "admin";
    string user;//read from file
    string pass;//read from file

    ifstream openfile("username.txt");
    cout << "Enter your username:";
    cin >> usertype;
    cout << "Enter your password:";
    cin >> passtype;

    bool found = false;
    while (found&&getline(openfile, line))
    {
        stringstream iss(line);
        getline(iss, user, ',');
        getline(iss,pass);
        if (usertype == manager && passtype == managerp)//admin login
        {
            void printoptionsadmin();
            found = true;
            break;
        }
        else if (usertype== user && passtype== pass)//regular login
            void printoptions();
        else
        {
            cout << "Invalid username or password, please start over." << endl;
            goto stop;//going back to login screen
        }
        openfile.close();
    }

}

2 个答案:

答案 0 :(得分:0)

每个没有第三个参数的getline()都会读取一个新行,它会为你提供一些你不需要的东西。

所以使用&#34; &#34;空格作为getline()函数中的分隔符而不是默认值&#34; \ n&#34;。

另一件事是while loop condition is always false found is initialized to false以来bool found = false; while (found&&getline(openfile, line)) bool found = false; std::string delimiter = ","; while (getline(openfile, line, " "))//To read upto each " "(white space instead of next line(\n is default third parameter) { /* Splitting the UserName & Password using ','*/ user = line.substr(0, line.find(delimiter)); pass = line.substr(line.find(delimiter)+1, -1); if (usertype == manager && passtype == managerp)//admin login { void printoptionsadmin(); found = true; break; } else if (usertype== user && passtype== pass)//regular login void printoptions(); else { cout << "Invalid username or password, please start over." << endl; goto stop;//going back to login screen } openfile.close(); } 。所以它不会在循环中进入

这是你需要的逻辑,

{{1}}

答案 1 :(得分:0)

我稍微更改了你的代码,你搞砸了found,加上我删除了goto,你也不必显式关闭文件,当范围剩下时它会被关闭(RAAI)。

我建议你在你的代码中添加一些检查(文件好吗?读取行吗?...)

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

static const string PASSWORD_FILE("username.txt");
static const string MANAGER_USER = "admin";
static const char DELIM = ',';

void printoptionsadmin() {
    cout << "Please select an option:" << endl;
    cout << "1.Sell Stock\n2.Buy stock\n3.Inquiry\n4.Logout\n5.Shutdown"
            << endl;
}
void printoptions() {
    cout << "Please select an option:" << endl;
    cout << "1.Sell Stock\n2.Buy stock\n3.Inquiry\n4.Logout" << endl;
}

string login() {
    ifstream passwordFile(PASSWORD_FILE);

    string usertype; //user input
    string passtype; //user input

    cout << "Enter your username:";
    cin >> usertype;
    cout << "Enter your password:";
    cin >> passtype;

    for (string userPasswordLine; getline(passwordFile, userPasswordLine);) {
        stringstream ss(userPasswordLine);
        string user, password;
        getline(ss, user, DELIM);
        getline(ss, password, DELIM);
        if (user == usertype && password == passtype) {
            return user;
        } // if
    } // for

    return "";
}

int main() {
    cout << "Please login." << endl;

    string loggedInUser;
    while ((loggedInUser = login()) == "") {
        cerr << "Login failed" << endl;
    } // while

    if (loggedInUser == MANAGER_USER) {
        printoptionsadmin();
    } else {
        printoptions();
    } // else

    return 0;
}
相关问题