//Book.h #pragma once #include <chrono> #include <ctime> #include <fstream> #include <iostream> #include <jsoncpp/json/json.h> #include <iomanip> #include <string.h> #include <string> #include <uuid/uuid.h> using namespace std; using namespace std::chrono; class Book { public: static char *uuidValue; static char *dtValue; char *getUuidValue(); string getTimeNow(); uint64_t Idx; char *Abstrct; char *Author; char *Comment; char *Content; char *Name; char *Summary; char *Title; char *Topic; void serializeBookVector(vector<Book> &vec,Json::Value &root); void fillBookVector(vector<Book>&vec,int len); void logFile(string fileName,string msg); void serializeBookVectorDemo(int len); }; //Model/Book.cpp #include "Model/Book.h" char* Book::uuidValue=(char*)malloc(40); char* Book::dtValue=(char*)malloc(20); string Book::getTimeNow() { time_point now=chrono::system_clock::now(); chrono::milliseconds ms=duration_cast<chrono::milliseconds>(now.time_since_epoch())%1000; time_t timer=system_clock::to_time_t(now); std::tm tmInfo=*localtime(&timer); stringstream oss; oss<<std::put_time(&tmInfo,"%Y%m%d%H%M%S")<<std::setfill('0')<<std::setw(3)<<ms.count(); return oss.str(); } char* Book::getUuidValue() { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID,uuidValue); return uuidValue; } void Book::serializeBookVector(vector<Book> &vec,Json::Value &root) { int len=vec.size(); for(int i=0;i<len;i++) { Json::Value jsonBook; jsonBook["Idx"]=Json::Value::UInt64(Idx); jsonBook["Abstract"]=vec[i].Abstrct; jsonBook["Author"]=vec[i].Author; jsonBook["Comment"]=vec[i].Comment; jsonBook["Content"]=vec[i].Content; jsonBook["Name"]=vec[i].Name; jsonBook["Summary"]=vec[i].Summary; jsonBook["Title"]=vec[i].Title; jsonBook["Topic"]=vec[i].Topic; root.append(jsonBook); free(vec[i].Abstrct); free(vec[i].Author); free(vec[i].Comment); free(vec[i].Content); free(vec[i].Name); free(vec[i].Summary); free(vec[i].Title); free(vec[i].Topic); } } void Book::fillBookVector(vector<Book>&vec,int len) { for(int i=0;i<len;i++) { Book bk; bk.Idx=(uint64_t)i*i*i*i*i; bk.Abstrct=(char*)malloc(40); bk.Author=(char*)malloc(40); bk.Comment=(char*)malloc(40); bk.Content=(char*)malloc(40); bk.Name=(char*)malloc(40); bk.Summary=(char*)malloc(40); bk.Title=(char*)malloc(40); bk.Topic=(char*)malloc(40); strcpy(bk.Abstrct,getUuidValue()); strcpy(bk.Author,getUuidValue()); strcpy(bk.Comment,getUuidValue()); strcpy(bk.Content,getUuidValue()); strcpy(bk.Name,getUuidValue()); strcpy(bk.Summary,getUuidValue()); strcpy(bk.Title,getUuidValue()); strcpy(bk.Topic,getUuidValue()); vec.push_back(bk); } } void Book::logFile(string fileName,string msg) { fstream wFile(fileName,ios::app); if(!wFile.is_open()) { cout<<"Create or open "<<fileName<<" failed!"<<endl; return; } wFile<<msg<<endl<<endl; wFile.close(); cout<<getTimeNow()<<", finished in "<<__FUNCTION__<<","<<__LINE__<<endl; } void Book::serializeBookVectorDemo(int len) { vector<Book> vec; fillBookVector(std::ref(vec),len); Json::Value root; serializeBookVector(std::ref(vec),std::ref(root)); Json::StyledWriter writer; string jsonValue= writer.write(std::ref(root)); string fileName(getTimeNow()); fileName=fileName.append(".txt"); logFile(fileName,jsonValue); cout<<getTimeNow()<<",finished in "<<__FUNCTION__<<","<<__LINE__<<endl; }
//main.cpp #include "Model/Book.h" int main(int args,char **argv) { Book bk; bk.serializeBookVectorDemo(atoi(argv[1])); }
Compile
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -ljsoncpp
标签:std,via,int,into,serialize,char,Book,vec,include From: https://www.cnblogs.com/Fred1987/p/16974562.html