如何在不使用C ++中的向量的情况下分隔逗号分隔值?

时间:2017-07-10 16:20:30

标签: c++ file-handling

我正在使用C ++编写ATM机器代码。我知道最好使用二进制文件和类,以便每个帐户在文件中具有完全相同的大小,即使它是空的。我正在寻找一些非常简单的东西。

我在解析.txt文件中的逗号分隔值时遇到问题。有时候我会得到价值,有时候我只得到第一个价值。我花了好几天才弄清楚它的解决方案。我搜索了互联网和stackoverflow几乎每个答案都使用了矢量或其他东西。

我没有使用strcmp进行数组比较,而是创建了自己的函数来比较字符和字符。

从代码的外观来看,似乎我做了很多,只需要一点点推动。请仔细看看我的代码,让我知道我错在哪里。我想消除语义错误!并修改以下代码以实现预期结果。

#include<iostream>
#include<conio.h>
#include<fstream>
#include<cstring>
#include<string>
#include<sstream>
#include<stdlib.h>

using namespace std;

void checkBalance();
void createAccount();
void deposit();
void withdraw();
void transfer();
void login();
void mainScreen();
void menuScreen();
// For file handling
void saveAccountToFile();
bool checkAccountExists(char*);
void loadAccount(char *);
void loadBeneficiary(char*);

bool compare(char *, char *);

const char * FILE_NAME = "accounts.txt";

char username[50];
char pin[5];
double balance = 0.0;

// Beneficiary Variables

char b_username[50];
char b_pin[5];
double b_balance = 0.0;

void menuScreen()
{
    char opt;
    do {
    system("cls");
    cout << "\n\n\t\t CMD - ATM";
    cout << "\n\n\tMENU";
    cout << "\n\n\t01. Check Balance";
    cout << "\n\n\t02. Withdraw";
    cout << "\n\n\t03. Deposite";
    cout << "\n\n\t04. Transfer to Another account";
    cout << "\n\n\t05. Logout";
    opt = _getch();
    switch (opt)
    {
    case '1':
        checkBalance();
        break;
    case '2':
        withdraw();
        break;
    case '3':
        deposite();
        break;
    case '4':
        transfer();
    case '5':
        cout << "\n\nThanks for using CMD ATM. Press any key to exit...";
        _getch();
        exit(1);
    default:
        cout << "\a\n\nIncorrect input. Press any key to try agian!";
        _getch();
    }
} while (opt != '5');
}

void mainScreen() 
{
char opt;
do {
    system("cls");
    cout << "\n\n\t\t CMD - ATM";
    cout << "\n\n\tMENU";
    cout << "\n\n\t01. Create Account";
    cout << "\n\n\t02. Login";
    cout << "\n\n\t03. Exit";
    opt = _getch();
    switch (opt)
    {
    case '1':
        createAccount();
        break;
    case '2':
        login();
        break;
    case '3':
        exit(1);
    default:
        cout << "\a\n\nIncorrect input. Press any key to try agian!";
        _getch();
    }
} while (opt != '3');
}

void saveAccountToFile() {

ofstream outFile;
outFile.open(FILE_NAME, ios::app);
if (outFile.is_open()) {
        outFile << username << ',' <<
            pin << ',' << balance << "\n";
}
outFile.close();
}

bool checkAccountExists(char * userName)
{   
int i = 0;

char temp[50] = {'0'};
ifstream inFile;
string line;
inFile.open(FILE_NAME);

if (inFile.is_open()) {
    while (getline(inFile, line)) {
        stringstream ss(line);
        string value;
        while (getline(ss, value, ',')) {
            if (i == 0) {
                strcpy(temp,value.c_str());
                if(compare(userName, temp)){
                    cout << "closing";
                    getchar();
                    inFile.close();
                    return true;
                }
                i = 1;
            }
            else if (i == 1) {
                i = 2;
            }
            else if (i == 2) {
                i = 0;
            }       
        }
    }
    inFile.close();
}
return false;
}


void loadAccount(char * userName)
{

int i = 0;
ifstream inFile;
string line;
inFile.open(FILE_NAME);

if (inFile.is_open()) {
    while (getline(inFile, line)) {
        stringstream ss(line);
        string value;
        while (getline(ss, value, ',')) {
            if (i == 0) {
                strcpy(username, value.c_str());
                i = 1;
            }
            else if (i == 1) {
                strcpy(pin, value.c_str());
                i = 2;
            }
            else if (i == 2) {
                balance = atof(value.c_str());
                i = 0;
            }
            if(compare(username,userName))
            {
                inFile.close();
                break;
            }       
        }
    }
    inFile.close();
}
}

void loadBeneficiary(char * userName)
{

int i = 0;
ifstream inFile;
string line;
inFile.open(FILE_NAME);

if (inFile.is_open()) {
    while (getline(inFile, line)) {
        stringstream ss(line);
        string value;
        while (getline(ss, value, ',')) {
            if (i == 0) {
                strcpy(b_username, value.c_str());
                i = 1;
            }
            else if (i == 1) {
                strcpy(b_pin, value.c_str());
                i = 2;
            }
            else if (i == 2) {
                b_balance = atof(value.c_str());
                i = 0;
            }   
            if(strcmp(b_username, userName)){
                inFile.close();
                break;
            }   
        }
    }
    inFile.close();
}
}

void login()
{
char tempName[50];
char tempPin[5];
cout << "\n\nEnter user name: ";
cin.getline(tempName, sizeof(tempName));
if (checkAccountExists(tempName)) {
    cout << "\n\nEnter user pin: ";
    cin.getline(tempPin,sizeof(tempPin));
    loadAccount(tempName);
    if (compare(tempPin, pin)) {
        cout << "\n\nLog in successfull!";
        cout << "\n\nPress any key to continue...";
        _getch();
        menuScreen();
    }else {
        cout << "User name or pin incorrect!";
        _getch();
    }
}
else {
    cout << "\n\nRecord not found. Press any key to continue...";
    _getch();
}

}

void checkBalance()
{
system("cls");
cout << "\n\n\tUser name = " << username;
cout << "\n\n\tBalance = " << balance;
_getch();
}

bool compare(char * msg1, char * msg2){
int count = 0;
int size = sizeof(msg1);
if(sizeof(msg2) == size){
    for(int i = 0; i < size; i++){
        if(msg1[i]==msg2[i]){
            count++;
        }
    }
    if(count == size)
        return true;
}

return false;
}

int main(int argc, char** argv) {

mainScreen();
getchar();
return 0;
}

TXT FILE

jhon,5155,99999.99
bot,4414,232323
theta,2111,34234

2 个答案:

答案 0 :(得分:1)

关于如何简化阅读循环,如何

while (getline(inFile, line)) {
    stringstream ss(line);
    string value;

    getline(ss, value, ',');
    string name = value;

    getline(ss, value, ',')
    string pin = value;

    getline(ss, value)
    double balance = stod(value);

    // Now use the name, pin and balance some way...
}

当你只需要这个名字时,你不需要进行第二次和第三次getline通话,只需要第一次。

还可以考虑使用结构来存储名称,引脚和平衡,然后使用container(例如vector)来存储结构。

同样以简洁的名义,我建议你只阅读文件一次。然后遍历向量以找到所需的数据。

答案 1 :(得分:0)

以下是不使用stringstream

的替代方法
while(getline(inFile,line,',')){

        strcpy(username, line.c_str());

        getline(inFile,line,',');
        strcpy(pin, line.c_str());

        getline(inFile,line,'\n');
        balance = atof(line.c_str());

        if(compare(username,userName)){
            inFile.close();
            break;
        }else 
            continue;

    }