Advertisement
ESHagibalov

чекни у себя пж

Oct 13th, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. long long files_size(std::string file_location, std::ifstream& in) {
  4. long long file_size;
  5. in.exceptions(std::ifstream::badbit | std::ifstream::failbit);
  6. try {
  7. in.open(file_location, std::ios::binary | std::ios::ate);
  8. file_size = in.tellg();
  9. in.close();
  10. return file_size;
  11. } catch (const std::exception &ex) {
  12. std::cout << "Error: ";
  13. std::cout << ex.what() << std::endl;
  14. }
  15. }
  16.  
  17.  
  18. int file_type(std::string file_location) {
  19. int size = file_location.size();
  20. int point_loc = 0;
  21. std::string type;
  22. for (int i = size - 1; i >= 0; i--)
  23. if (file_location.at(i) == '.') {
  24. point_loc = i;
  25. break;
  26. }
  27. for (int i = point_loc + 1; i < size; i++)
  28. type += file_location.at(i);
  29. std::string type_str[8] = { "doc" , "txt", "mp3", "mp4", "bin", "jpg", "png", "cpp" };
  30. for (int i = 0; i < 8; i++)
  31. if (type == type_str[i]) {
  32. std::cout << type;
  33. return i;
  34. }
  35. }
  36.  
  37. void file_types_to_bit(char files_types[], std::string* file_location, short count) {
  38. for (int i = 0; i < count; i++) {
  39. if (i % 2 == 0)
  40. files_types[i / 2] = file_type(file_location[i]) << 4;
  41. else
  42. files_types[i / 2] += file_type(file_location[i]);
  43. }
  44. }
  45.  
  46. long long min(long long a, long long b){
  47. return a < b ? a : b;
  48. }
  49. int main() {
  50. std::ifstream in;
  51. std::ofstream out;
  52.  
  53. short count_of_files;
  54.  
  55. std::cin >> count_of_files;
  56. std::string files_location[count_of_files];
  57. long long files_sizes[count_of_files];
  58. char extensions_codes[16];
  59.  
  60. for(int i = 0; i < count_of_files; i ++) {
  61. std::cin >> files_location[i];
  62. }
  63.  
  64. for(int i = 0; i < count_of_files; i ++) {
  65. files_sizes[i] = files_size(files_location[i], in);
  66. }
  67.  
  68. file_types_to_bit(extensions_codes, files_location, count_of_files);
  69.  
  70.  
  71. out.open("/home/ernest/Documents/out_file.bin", std::ios::binary);
  72.  
  73. out.write(reinterpret_cast<const char *>(&count_of_files), 2);
  74. out.write(reinterpret_cast<const char *>(files_sizes), 8 * count_of_files);
  75. out.write(extensions_codes, 8);
  76.  
  77. for(int i = 0; i < count_of_files; i ++) {
  78. //char *data_from_files = new char[files_sizes[i]];
  79. in.open(files_location[i], std::ios::binary);
  80. while(!in.eof()) {
  81. char buffer_r[1024];
  82. in.read(buffer_r, 1024);
  83. int realRead = in.gcount();
  84. out.write(buffer_r, realRead);
  85. }
  86. //out.write(data_from_files, files_sizes[i]);
  87. in.close();
  88. //delete[] data_from_files;
  89. }
  90. out.close();
  91.  
  92. return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement