// DatProecdure.h#pragma once#include <string>#include<fstream>#include<sstream>#include <iomanip>class DatProcedure{public: DatProcedure(const std::string& filePath) { std::ifstream infile(filePath, std::ios::binary); infile.seekg(0, infile.end); fileSize = infile.tellg(); infile.seekg(0, infile.beg); file = filePath; char* buf = new char[fileSize]; infile.read(static_cast<char*>(buf), fileSize); content = buf; infile.close(); } std::string readBin() { return content; }; std::string convertToHexLitera(const unsigned& decimal) { std::stringstream ss; //setw 显示宽度 ss << "0x" << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << decimal; std::string result = ss.str(); ss.clear(); return result; } std::string convertToHexLitera(const int& beg, const int& len) { unsigned decimal = convertToDecimal(beg, len); std::string res = convertToHexLitera(decimal); return res; } unsigned convertToDecimal(const std::string& binary) { unsigned res = 0; for (int i = 0; i < binary.size(); i++) { res <<= 1; res += (binary[i] - '0'); } return res; } unsigned convertToDecimal(const int& beg, const int& len) { const std::string binary= content.substr(beg, len); unsigned res = convertToDecimal(binary); return res; }private: std::string file; unsigned fileSize; std::string content;};
#include<iostream>#include <io.h>#include<windows.h>#include"DatProcedure.h"#include<vector>void getDatFiles(std::string path, std::vector<std::string>& files){ long hFile = 0; struct _finddata_t fileinfo; std::string p; if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { std::string fileName = ""; do { if (!(fileinfo.attrib & _A_SUBDIR)) { fileName = fileinfo.name; if (fileName.substr(fileName.size() - 4, 4) == ".dat") { files.push_back(fileinfo.name); } } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); }}int main(){ std::vector<std::string> files; getDatFiles(".\\", files); for (auto& f : files) { std::cout << f << std::endl; } std::cout << "Do you want to process the above documents ?(y/n, Y/N)" << std::endl; std:: string input = ""; std::cin >> input; if (input == "y" || input == "yes" || input == "Y") { std::cout << "start to handle !" << std::endl; for (int i = 0; i < files.size(); i++) { std::string filePath = files[i]; DatProcedure handleIPV6(filePath); std::string out = handleIPV6.readBin(); std::cout << handleIPV6.convertToHexLitera(0, 4); for (int j = 0; j < out.size(); j++) { std::cout << out[j]; } } } // 写入到csv std::ofstream outFile; outFile.open("data.csv", std::ios::out); // 打开模式可省略 outFile << "name" << ',' << "age" << ',' << "hobby" <<std::endl; outFile << "Mike" << ',' << 18 << ',' << "paiting" << std::endl; outFile << "Tom" << ',' << 25 << ',' << "football" << std::endl; outFile << "Jack" << ',' << 21 << ',' << "music" << std::endl; outFile.close(); system("pause"); return 0;}