我目前正在完成我的学校考试项目。我要创建一个用户数据库,您可以在其中通过帐户登录并向系统中的其他人发送消息。但是有几个错误,我直到现在才发现。程序第一次执行时,会将用户数据库复制成.txt格式,像这样(userlist.txt),
created: Sun May 13 18:41:08 2012
mod_date: Sun May 13 18:41:08 2012
ID:1
created:Sun May 13 18:41:08 2012
name:admin
password:Admin1
security level:2
status:active
这很好用,因为它是从向量复制的。但是我必须做相反的过程,当我第二次打开程序时(除非我删除了 userlist.txt),并且向量似乎包含错误的信息。很难解释,因此我将源代码上传到了 mediafire。我猜总共有 600 行代码,但我只认为 main.cpp 以及 dbmani.h 中的函数 login() 和 writedb() (其中向量是在类中定义的)是查看问题所必需的. 代码的第一行在这里(如果存在,向量将从 userlist.txt 接收输入),希望您不必下载我所有的源代码,
int main()
{
system("mkdir messages");
ifstream inFile;
inFile.open("userlist.txt");
if(inFile.good()) // If userlist.txt exist - therefore an existing user database exist.
{
// ADD DATE OF MODIFICATION
cout << "USERLIST FOUND, READING USERS.\n\n";
userstats tempBuffer;
int userCount = -1;
int overCount = 0;
string buffer;
while(getline(inFile, buffer))
{
if (0 == buffer.find("ID:"))
{
userCount++;
if (userCount > overCount)
{
userbase.users.push_back(tempBuffer);
overCount++;
}
buffer.erase(0, 3);
tempBuffer.ID = buffer;
}
else if (0 == buffer.find("created:"))
{
buffer.erase(0, 8);
tempBuffer.date = buffer;
}
else if (0 == buffer.find("name:"))
{
buffer.erase(0, 5);
tempBuffer.name = buffer;
}
else if (0 == buffer.find("password:"))
{
buffer.erase(0, 9);
tempBuffer.password = buffer;
}
else if (0 == buffer.find("status:"))
{
buffer.erase(0,7);
if (buffer == "active")
tempBuffer.active = true;
else if (buffer == "inactive")
tempBuffer.active = false;
}
}
if (userCount == 0)
{
userbase.users.push_back(tempBuffer);
}
inFile.close();
}
else // If no userlist.txt exists, create new one from vector. This instance works, but there is only one user
{
cout << "NO USERS FOUND, CREATING NEW LIST.\n";
inFile.close();
// Determine current date
time_t rawtime;
struct tm * timeinfo;
time (&rawtime );
timeinfo = localtime ( &rawtime );
string cdate = asctime(timeinfo);
userstats userBuffer = {"1", cdate, "admin", "Admin1", admin, 1};
userbase.users.push_back(userBuffer);
ofstream outFile("userlist.txt");
outFile << "created: " << cdate;
outFile << "mod_date: " << cdate << "\n\n";
outFile << "ID:" << userbase.users[0].ID << "\n";
outFile << "created:" << userbase.users[0].date;
outFile << "name:" << userbase.users[0].name << "\n";
outFile << "password:"<< userbase.users[0].password << "\n";
outFile << "security level:" << userbase.users[0].secLev << "\n";
outFile << "status:active\n\n";
outFile.close();
}
// CHECK DIR
char ch;
cout << "Choices are listed below\n";
cout << "1) login\t2) Register an Account\n"
<< "q) Quit.\n";
cout << "Select option: ";
bool breakFlag = false;
while (breakFlag == false && cin.get(ch))
{
cin.ignore();
// Could use getch to eliminate errors instead.
switch (ch)
{
case '1': int logID;
logID = userbase.login();
if (logID)
{
logID -= 1;
loggedin(logID);
}
...
另外,如果我在 dbmani.h 中使用 regAcc() 注册了第二个帐户“admina”,我注意到我的用户列表看起来像这样(创建第三个帐户将覆盖第二个帐户),
created: Sun May 13 18:41:08 2012
mod_date: Sun May 13 19:08:01 2012
ID:1
created:Sun May 13 18:41:08 2012name:admin
password:Admin1
status:active
ID:2
created:Sun May 13 19:08:01 2012
name:admina
password:Admin1
security level:0
status:active
这里是源代码,如果你喜欢它: http ://www.mediafire.com/download.php?6dp6sxn952j7s37
如果有人愿意花一些时间帮助我,我将非常感激,如果只是解决“.txt to vector”问题,因为我的时间不多了,而且我真的看不到错误。我试着在网上寻找,但我似乎没有找到任何关于它的东西。