1. // DatProecdure.h
    2. #pragma once
    3. #include <string>
    4. #include<fstream>
    5. #include<sstream>
    6. #include <iomanip>
    7. class DatProcedure
    8. {
    9. public:
    10. DatProcedure(const std::string& filePath) {
    11. std::ifstream infile(filePath, std::ios::binary);
    12. infile.seekg(0, infile.end);
    13. fileSize = infile.tellg();
    14. infile.seekg(0, infile.beg);
    15. file = filePath;
    16. char* buf = new char[fileSize];
    17. infile.read(static_cast<char*>(buf), fileSize);
    18. content = buf;
    19. infile.close();
    20. }
    21. std::string readBin()
    22. {
    23. return content;
    24. };
    25. std::string convertToHexLitera(const unsigned& decimal) {
    26. std::stringstream ss;
    27. //setw 显示宽度
    28. ss << "0x" << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << decimal;
    29. std::string result = ss.str();
    30. ss.clear();
    31. return result;
    32. }
    33. std::string convertToHexLitera(const int& beg, const int& len) {
    34. unsigned decimal = convertToDecimal(beg, len);
    35. std::string res = convertToHexLitera(decimal);
    36. return res;
    37. }
    38. unsigned convertToDecimal(const std::string& binary) {
    39. unsigned res = 0;
    40. for (int i = 0; i < binary.size(); i++) {
    41. res <<= 1;
    42. res += (binary[i] - '0');
    43. }
    44. return res;
    45. }
    46. unsigned convertToDecimal(const int& beg, const int& len) {
    47. const std::string binary= content.substr(beg, len);
    48. unsigned res = convertToDecimal(binary);
    49. return res;
    50. }
    51. private:
    52. std::string file;
    53. unsigned fileSize;
    54. std::string content;
    55. };
    1. #include<iostream>
    2. #include <io.h>
    3. #include<windows.h>
    4. #include"DatProcedure.h"
    5. #include<vector>
    6. void getDatFiles(std::string path, std::vector<std::string>& files)
    7. {
    8. long hFile = 0;
    9. struct _finddata_t fileinfo;
    10. std::string p;
    11. if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    12. {
    13. std::string fileName = "";
    14. do
    15. {
    16. if (!(fileinfo.attrib & _A_SUBDIR))
    17. {
    18. fileName = fileinfo.name;
    19. if (fileName.substr(fileName.size() - 4, 4) == ".dat") {
    20. files.push_back(fileinfo.name);
    21. }
    22. }
    23. } while (_findnext(hFile, &fileinfo) == 0);
    24. _findclose(hFile);
    25. }
    26. }
    27. int main()
    28. {
    29. std::vector<std::string> files;
    30. getDatFiles(".\\", files);
    31. for (auto& f : files) {
    32. std::cout << f << std::endl;
    33. }
    34. std::cout << "Do you want to process the above documents ?(y/n, Y/N)" << std::endl;
    35. std:: string input = "";
    36. std::cin >> input;
    37. if (input == "y" || input == "yes" || input == "Y") {
    38. std::cout << "start to handle !" << std::endl;
    39. for (int i = 0; i < files.size(); i++) {
    40. std::string filePath = files[i];
    41. DatProcedure handleIPV6(filePath);
    42. std::string out = handleIPV6.readBin();
    43. std::cout << handleIPV6.convertToHexLitera(0, 4);
    44. for (int j = 0; j < out.size(); j++) {
    45. std::cout << out[j];
    46. }
    47. }
    48. }
    49. // 写入到csv
    50. std::ofstream outFile;
    51. outFile.open("data.csv", std::ios::out); // 打开模式可省略
    52. outFile << "name" << ',' << "age" << ',' << "hobby" <<std::endl;
    53. outFile << "Mike" << ',' << 18 << ',' << "paiting" << std::endl;
    54. outFile << "Tom" << ',' << 25 << ',' << "football" << std::endl;
    55. outFile << "Jack" << ',' << 21 << ',' << "music" << std::endl;
    56. outFile.close();
    57. system("pause");
    58. return 0;
    59. }