Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- long long files_size(std::string file_location, std::ifstream& in) {
- long long file_size;
- in.exceptions(std::ifstream::badbit | std::ifstream::failbit);
- try {
- in.open(file_location, std::ios::binary | std::ios::ate);
- file_size = in.tellg();
- in.close();
- return file_size;
- } catch (const std::exception &ex) {
- std::cout << "Error: ";
- std::cout << ex.what() << std::endl;
- }
- }
- int file_type(std::string file_location) {
- int size = file_location.size();
- int point_loc = 0;
- std::string type;
- for (int i = size - 1; i >= 0; i--)
- if (file_location.at(i) == '.') {
- point_loc = i;
- break;
- }
- for (int i = point_loc + 1; i < size; i++)
- type += file_location.at(i);
- std::string type_str[8] = { "doc" , "txt", "mp3", "mp4", "bin", "jpg", "png", "cpp" };
- for (int i = 0; i < 8; i++)
- if (type == type_str[i]) {
- std::cout << type;
- return i;
- }
- }
- void file_types_to_bit(char files_types[], std::string* file_location, short count) {
- for (int i = 0; i < count; i++) {
- if (i % 2 == 0)
- files_types[i / 2] = file_type(file_location[i]) << 4;
- else
- files_types[i / 2] += file_type(file_location[i]);
- }
- }
- long long min(long long a, long long b){
- return a < b ? a : b;
- }
- int main() {
- std::ifstream in;
- std::ofstream out;
- short count_of_files;
- std::cin >> count_of_files;
- std::string files_location[count_of_files];
- long long files_sizes[count_of_files];
- char extensions_codes[16];
- for(int i = 0; i < count_of_files; i ++) {
- std::cin >> files_location[i];
- }
- for(int i = 0; i < count_of_files; i ++) {
- files_sizes[i] = files_size(files_location[i], in);
- }
- file_types_to_bit(extensions_codes, files_location, count_of_files);
- out.open("/home/ernest/Documents/out_file.bin", std::ios::binary);
- out.write(reinterpret_cast<const char *>(&count_of_files), 2);
- out.write(reinterpret_cast<const char *>(files_sizes), 8 * count_of_files);
- out.write(extensions_codes, 8);
- for(int i = 0; i < count_of_files; i ++) {
- //char *data_from_files = new char[files_sizes[i]];
- in.open(files_location[i], std::ios::binary);
- while(!in.eof()) {
- char buffer_r[1024];
- in.read(buffer_r, 1024);
- int realRead = in.gcount();
- out.write(buffer_r, realRead);
- }
- //out.write(data_from_files, files_sizes[i]);
- in.close();
- //delete[] data_from_files;
- }
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement