Advertisement
SilverTES

Manage File : c++

Jan 14th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5.  
  6. std::string memoryData(std::ifstream &file, int startAt, int stopAt, bool show = false)
  7. {
  8.     std::string data;
  9.  
  10.     int memorySize = stopAt - startAt;
  11.  
  12.     file.seekg(startAt);
  13.     data.resize(memorySize);
  14.     file.read(&data[0], memorySize);
  15.  
  16.     if (show)
  17.         std::cout << data;
  18.  
  19.     return data;
  20. }
  21.  
  22. std::string memoryData(std::fstream &file, int startAt, int stopAt, bool show = false)
  23. {
  24.     std::string data;
  25.  
  26.     int memorySize = stopAt - startAt;
  27.  
  28.     file.seekg(startAt);
  29.     data.resize(memorySize);
  30.     file.read(&data[0], memorySize);
  31.  
  32.     if (show)
  33.         std::cout << data;
  34.  
  35.     return data;
  36.  
  37. }
  38.  
  39. std::ifstream::pos_type fileSize(const char* filename)
  40. {
  41.     std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
  42.     return in.tellg();
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48.  
  49.     std::ifstream readA("myanimation.json", std::ios::in | std::ios::binary);
  50.     std::ifstream readB("myanimation.png", std::ios::in | std::ios::binary);
  51.  
  52.     std::ofstream writeAB("myanimation.mas", std::ios::out | std::ios::binary);
  53.  
  54.     writeAB << readA.rdbuf() << "[END_OF_JSON]" << readB.rdbuf() << "[END_OF_PNG]";
  55.  
  56.     readA.close();
  57.     readB.close();
  58.     writeAB.close();
  59.  
  60.     std::ifstream readMAS("myanimation.mas", std::ios::in | std::ios::binary);
  61.  
  62.     std::ofstream writeJSON("myanimationClone.json", std::ios::out | std::ios::binary);
  63.     std::ofstream writePNG("myanimationClone.png", std::ios::out | std::ios::binary);
  64.  
  65.     std::string memoryPNG;
  66.     std::string memoryJSON;
  67.  
  68.     if (readMAS.is_open())
  69.     {
  70.         writeJSON << memoryData(readMAS, 0, 276);
  71.         writePNG << memoryData(readMAS, 289, 30908);
  72.     }
  73.  
  74.     readMAS.close();
  75.  
  76.     writeJSON.close();
  77.     writePNG.close();
  78.  
  79.  
  80.     //std::ifstream fileTEST("myanimationClone.json", std::ios::in | std::ios::binary);
  81.  
  82.     std::fstream fileTEST("test.txt", std::ios::in | std::ios::out | std::ios::binary);
  83.  
  84.     std::string endOfJSON = "<END_OF_JSON>";
  85.  
  86.     int pos = 0;
  87.     int sizeFile = 0;
  88.  
  89.     if (fileTEST.is_open())
  90.     {
  91.  
  92.         // Write in file !
  93.         fileTEST << "I am" << std::endl;
  94.         fileTEST << "a JSON file" << "\n";
  95.         fileTEST << "who contain" << "\n";
  96.         fileTEST << "Good" << "\n";
  97.         fileTEST << "Animation" << "\n";
  98.         fileTEST << endOfJSON+"\n";
  99.         fileTEST << "here is the PNG DATA\n";
  100.         fileTEST << "here is the PNG DATA\n";
  101.         fileTEST << "here is the PNG DATA\n";
  102.         fileTEST << "here is the PNG DATA\n";
  103.  
  104.  
  105.         // Read in file
  106.         std::string line;
  107.  
  108.         fileTEST.seekg(0);
  109.  
  110.  
  111.         while (std::getline(fileTEST, line))
  112.         {
  113.             //std::cout << "Position : " << fileTEST.tellg() << "\n";
  114.             std::cout << line << "\n";
  115.             //std::cout << "Position : " << fileTEST.tellg() << "\n";
  116.         }
  117.  
  118.         fileTEST.clear();
  119.  
  120.         fileTEST.seekg(0, fileTEST.beg);
  121.  
  122.         //std::cout << "Position : " <<fileTEST.tellg() << "\n";
  123.         sizeFile = fileSize("test.txt");
  124.         std::cout << "fileSize = " << sizeFile;
  125.  
  126.         while (fileTEST.good())
  127.         {
  128.             std::getline(fileTEST, line);
  129.  
  130.             //std::cout << line;
  131.  
  132.             if (line.find("<END_OF_JSON>") != std::string::npos)
  133.             {
  134.                 pos = fileTEST.tellg() - (endOfJSON.length()+1);
  135.                 std::cout << "--- Found at "<< pos <<"\n";
  136.             }
  137.  
  138. //           char ch;
  139. //           fileTEST.get(ch);
  140. //           std::cout << ch;
  141.         }
  142.  
  143.         fileTEST.clear();
  144.  
  145.         std::ofstream fileJSON("json.txt", std::ios::out | std::ios::binary);
  146.         std::ofstream filePNG("png.txt", std::ios::out | std::ios::binary);
  147.  
  148.         if (fileJSON.is_open())
  149.         {
  150.             fileJSON << memoryData(fileTEST, 0, pos);
  151.             fileJSON.close();
  152.         }
  153.         else
  154.             std::cout << "- fileJSON error !\n";
  155.  
  156.  
  157.         if (filePNG.is_open())
  158.         {
  159.             filePNG << memoryData(fileTEST, pos+(endOfJSON.length()+1), sizeFile);
  160.             filePNG.close();
  161.         }
  162.         else
  163.             std::cout << "- filePNG error !\n";
  164.  
  165.     }
  166.  
  167.     fileTEST.close();
  168.  
  169.     std::cout << "\n";
  170.     std::cout << "- Operation OK ! \n";
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement