【问题标题】:C++ Strings/ FilesC++ 字符串/文件
【发布时间】:2015-04-11 23:51:46
【问题描述】:

我正在尝试读取包含 SSN、用户名和密码列表的文件。我正在尝试用 x 替换除最后 4 个社交之外的所有内容,并且我有一个根据密码强度给出分数的系统(“累加器”用于这部分)。我无法从 main 输出带有 cout 的表格,希望能得到一些帮助。当我尝试从 main 输出它时,它只执行文件的最后一行,并且该行上的 SSN 不是我想要的 x。谢谢

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void openFile(ifstream&, string, string, string);
string xReplace(ifstream& myIn, string& social, string& username, string&     password, string socialRep);
void passStrength(int& count1, int& count2, int& count3, int& accumulator,   ifstream& myIn, string& social, string& username, string& password);


int main()
{
ifstream myIn;
string path, file;
string fileName;
string social;
string username;
string password;
string socialRep = "xxx-xx";
string oneLine = social + username + password;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int accumulator = 0;

openFile(myIn, path, file, fileName);
xReplace(myIn, social, username, password, socialRep);
passStrength(count1, count2, count3, accumulator, myIn, social, username, password);


myIn >> social >> username >> password >> accumulator;
while (myIn.good())

cout << "SSN\tUser Name\tPassword\tPassword Strength\n";
cout << "---------------------------------------------------------\n";
cout << social << " " << username << " " << password << " " << accumulator << "\n";


return 0;
}


void openFile(ifstream& myIn, string path, string file, string fileName)
{

path = "C:\\2430";
cout << "Enter the name of your file";
getline(cin, file);

fileName = path + "\\" + file;
cout << "The file name is " << fileName << endl;

myIn.open(fileName.c_str());
if (myIn.fail())
{
cout << "Filename was invalid\n";
exit(1);
}
cout << "The file" " " << file << "has been opened\n";

}


void passStrength(int& count1, int& count2, int& count3, int& accumulator, ifstream& myIn, string& social, string& username, string& password)
{

while (myIn >> social >> username >> password)
{
for (unsigned int i = 0; i > password.length(); i++)
if (password.length() > 8)

count1 = 1;

else count1 = 0;

int testing = -1;
testing = password.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

if (testing != password.npos)

count2 = 1;

else count2 = 0;

unsigned int testing2 = -1;
testing2 = password.find_first_of("!@#$%^&*()");
if (testing2 != password.npos)

count3 = 1;

else count3 = 0;

accumulator = count1 + count2 + count3;
//cout << social << " " << username << " " << password << " " <<accumulator<< "\n";


}
}

string xReplace(ifstream& myIn, string& social, string& username, string&    password, string socialRep)
{

myIn >> social >> username >> password;
social.replace(0, 6, socialRep);
return social;
}

【问题讨论】:

    标签: c++ string file


    【解决方案1】:

    您需要一个容器来保存您的程序中的记录。变量一次只能保存一个数据,

    您可以为以下变量社会、用户名和密码定义结构记录,如下所示:

    struct record{
       string social;
       string username;
       string password;
    };
    

    并创建一个 std::vector 如下

    std::vector<record> my_records;
    

    所以当你从文件中读取数据时,将数据插入到 my_record 中,如下所示。

    my_reccords.push_back(recod_data);
    

    并处理来自my_records 的记录,最后打印来自my_records 的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 2013-03-29
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多