[英]C++ Strings/ Files
我正在嘗試讀取一個包含SSN,用戶名和密碼列表的文件。 我試圖用x代替社交中的最后四個,但我有一個基於密碼強度給出分數的系統(此部分使用“累加器”)。 我在從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;
}
您需要一個容器來將記錄保留在程序中。 變量一次只能保存一個數據,
您可以為以下變量social,username和password定義一個結構記錄,如下所示:
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.