parent
a5801b7808
commit
d099adf73f
@ -0,0 +1,67 @@
|
||||
#include"filemanage.h"
|
||||
#include"tool.h"
|
||||
#include<fstream>
|
||||
#include<direct.h>
|
||||
#include<iostream>
|
||||
#include<dirent.h>
|
||||
|
||||
Filemanage::Filemanage(const std::string&dir):baseDir(dir){
|
||||
createDir(baseDir);
|
||||
}
|
||||
|
||||
void Filemanage::createDir(const std::string& path){
|
||||
_mkdir(path.c_str());
|
||||
}
|
||||
|
||||
std::string Filemanage::getUserFolder(const std::string& username){
|
||||
std::string userFolder = baseDir + "/" + username;
|
||||
createDir(userFolder);
|
||||
return userFolder;
|
||||
}
|
||||
|
||||
std::string Filemanage::saveQuestions(const std::string& username,const std::vector<Question> questions){
|
||||
std::string userFolder = getUserFolder(username);
|
||||
std::string filename = tool::getCurrentTime() + ".txt";
|
||||
std::string filepath = userFolder + "/" + filename;
|
||||
|
||||
std::ofstream userpath(filepath);
|
||||
if(!userpath.is_open()){
|
||||
std::cout<<"ÎÞ·¨´´½¨Îļþ"<<filepath<<std::endl;
|
||||
}
|
||||
|
||||
for(const Question question:questions){
|
||||
userpath<<question.id<<"."<<question.content<<std::endl<<std::endl;
|
||||
}
|
||||
|
||||
userpath.close();
|
||||
return filepath;
|
||||
}
|
||||
|
||||
std::set<std::string> Filemanage::getQuestions(const std::string&username){
|
||||
std::set<std::string>productedQuestion;
|
||||
std::string userFolder = getUserFolder(username);
|
||||
DIR* dirstream = opendir(userFolder.c_str());
|
||||
struct dirent* dir;
|
||||
while((dir = readdir(dirstream)) != NULL){
|
||||
std::string filename = dir->d_name;
|
||||
if(filename.length() > 4&&filename.substr(filename.length() - 4) == ".txt"){
|
||||
std::string filepath = userFolder + "/" + filename;
|
||||
std::ifstream userpath;
|
||||
userpath.open(filepath);
|
||||
if(userpath.is_open()){
|
||||
std::string OneQuestion;
|
||||
while(getline(userpath,OneQuestion)){
|
||||
OneQuestion = tool::EreaseEmpty(OneQuestion);
|
||||
if(!OneQuestion.empty()&&OneQuestion.find('.') != std::string::npos){
|
||||
int dotnum = OneQuestion.find('.');
|
||||
OneQuestion = OneQuestion.substr(dotnum + 1,OneQuestion.size() - dotnum - 1);
|
||||
productedQuestion.insert(OneQuestion);
|
||||
}
|
||||
}
|
||||
}
|
||||
userpath.close();
|
||||
}
|
||||
}
|
||||
closedir(dirstream);
|
||||
return productedQuestion;
|
||||
}
|
||||
Loading…
Reference in new issue