Передача свойств в текстовый вид

Прошу помощи! Данная программа шифрует/расшифрует (самым простым способом) и архивирует/разархивирует зашифрованный файл.

Просьба такая: при разархивации помимо основного расшифрованного файла создать ещё два текстовых документа, в один из которых записывается вес архива с зашифрованным документом, в другой число символов в зашифрованном документе.

Сам код:

#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <Windows.h>
#include <stdio.h>
#include <process.h>
#include <vector>
 
using namespace std;
 
inline unsigned char shifrator(char);
 
inline unsigned char shifrator(char chr)
{
    return (~chr);
}
 
int main(int argc, char** argv)
{
    setlocale(0, "Russian");
    ifstream filein;
    fstream fileout;
    string inputPath, outputPath, temp, toFlag, reName;
    string oldName;
    bool folderExist = FALSE;
    int result, result2;
 
    for (int i = 0; i < argc; i++) 
    {
        if (string(argv[i]) == "-archive" || string(argv[i]) == "-dearchive") {
            
            if (inputPath.empty()) 
            {
                if (i + 1 < argc) 
                {
                    inputPath = string(argv[i + 1]);
                    toFlag = string(argv[i]);
                }
            }
        }
        if (string(argv[i]) == "-folder") 
        {
            if (i + 1 < argc) {
                outputPath = string(argv[i + 1]);
                folderExist = TRUE;
            }
        }
 
        if (string(argv[i]) == "-rename")
        {
            if (i + 1 < argc)
            {
                reName = string(argv[i + 1]);
            }
        }
    }
 
    if (argc < 2)
    {
        string answer;
        cout << "-dearchive вытаскивание; -archive архивация \n" << endl;
        cin >> answer;
 
        if (answer == "-archive")
        {
            cout << "\nПуть к файлу" << endl;
            cin >> answer;
            inputPath = answer;
            toFlag = "-archive";
        }
        else if (answer == "-dearchive")
        {
            cout << "\nПуть к архиву" << endl;
            cin >> answer;
            inputPath = answer;
            toFlag = "-dearchive";
        }
 
        cout << "Введите 0 если хотите оставить в той же директории или -folder для пути" << endl;
        cin >> answer;
 
        if (answer == "-folder")
       {
            cout << "Введите путь" << endl;
            cin >> answer;
            if (answer != "0")
            {
                outputPath = answer;
                folderExist = TRUE;
            }
        }
 
         cout << "введите 0 если не хотите менять имя файла или -rename(смена)" << endl;
         cin >> answer;
 
         if (answer == "-rename")
         {
             cout << "Введите имя " << endl;
             cin >> answer;
             reName = answer;
 
         }
    }
 
    if (inputPath.empty()) 
    {
        cout << "Не указан файл архивации";
        return 0;
    }
 
    //temp
    temp = inputPath;
    temp = temp.erase(temp.find_last_of("."), temp.length() - temp.find_last_of(".")) + "temp.txt";
 
    if (toFlag == "-archive")
    {
        //check
        if (!folderExist) 
        {
            outputPath = inputPath;
            outputPath = outputPath.erase(outputPath.find_last_of("."), outputPath.length() - outputPath.find_last_of("."));
        }
        string cop;
        if (folderExist) 
        {
            cop = inputPath;
            cop = cop.erase(0, cop.find_last_of("\\"));
            outputPath += cop.erase(cop.find_last_of("."), cop.length() - cop.find_last_of("."));
 
        }
        cout << inputPath << endl;
        filein.open(inputPath, ios::in | ios::binary);
        fileout.open(temp, ios::out | ios::trunc | ios::binary);
 
        //Open
        if (filein.is_open())
        {
            cout << "Файл " << inputPath << " Файл открыт" << endl;
            if (fileout.is_open())
            {
                cout << "Файл " << temp << " Файл открыт" << endl;
                unsigned char chr = 0;
                cout << "Идёт шифровка/расшифровка" << endl;
                while (filein.read((char*)&chr, sizeof(char)))
                {
                    chr = shifrator(chr);
                    fileout.write((char*)&chr, sizeof(char));
                }
 
                //Close file
                filein.close();
                fileout.close();
                cout << reName << endl;
 
                //Delete and encryption
                remove(inputPath.c_str());
                if (reName.empty())
                {
                    result2 = rename(temp.c_str(), inputPath.c_str());
                }
                else 
                {
 
                    oldName = inputPath;
                    inputPath = inputPath.erase(inputPath.find_last_of("\\") + 1, inputPath.length() - inputPath.find_last_of("\\")) + reName;
 
                    cout << inputPath << endl;
 
                    result2 = rename(temp.c_str(), inputPath.c_str());
                }
                cout << "Процесс завершён!" << endl;
            }
            else
            {
                cout << "Не удалось открыть выходной файл " << temp << " !" << endl;
                return 0;
            }
        }
        else
        {
            cout << "Не удалось открыть входной файл" << inputPath << " !" << endl;
 
            return 0;
        }
 
        if (filein.is_open())
        {
            filein.close();
        }
        if (fileout.is_open())
        {
            fileout.close();
        }
 
        //Start process archive
        _spawnl(P_WAIT, "D:\\7zip\\7-Zip\\7z.exe", "7z", "a", "-tzip", outputPath.c_str(), inputPath.c_str(), NULL);
 
        if (!oldName.empty())
        {
            rename(inputPath.c_str(), oldName.c_str());
        }
 
    }
 
    else if (toFlag == "-dearchive")
    {
        string currFile;
        string out;
 
        if (!folderExist) {
            outputPath = inputPath;
            outputPath = outputPath.erase(outputPath.find_last_of("\\") + 1, outputPath.length() - outputPath.find_last_of("\\"));
        }
        out = "-o\"" + outputPath + "\"";
        //dearchive
        _spawnl(P_WAIT, "D:\\7zip\\7-Zip\\7z.exe", "7z","x", inputPath.c_str(), out.c_str(), NULL);
 
        //Имя архива = имя файла
        currFile = inputPath;
        currFile = currFile.erase(0, currFile.find_last_of("\\") + 1);
        currFile = currFile.erase(currFile.find_last_of("."), currFile.length() - currFile.find_last_of(".")) + ".txt";
        inputPath = outputPath + currFile;
 
        cout << inputPath << endl;
        // Модификаторы открытия
        filein.open(inputPath, ios::in | ios::binary);
        fileout.open(temp, ios::out | ios::trunc | ios::binary);
 
        //Open
        if (filein.is_open())
        {
            cout << "Файл " << inputPath << " Файл открыт" << endl;
            if (fileout.is_open())
            {
                cout << "Файл " << temp << " Файл открыт" << endl;
                unsigned char chr = 0;
                cout << "Идёт шифровка/расшифровка" << endl;
                while (filein.read((char*)&chr, sizeof(char)))
                {
                    chr = shifrator(chr);
                    fileout.write((char*)&chr, sizeof(char));
                }
 
                //Close file
                filein.close();
                fileout.close();
 
                //Delete file and encryption
                remove(inputPath.c_str());
                result2 = rename(temp.c_str(), inputPath.c_str());
 
                cout << result2 << endl;
 
            }
            else
            {
                cout << "Не удалось открыть выходной файл " << temp << " !" << endl;
                return 0;
            }
        }
        else
        {
            cout << "Не удалось открыть входной файл" << inputPath << " !" << endl;
 
            return 0;
        }
 
        if (filein.is_open())
        {
            filein.close();
        }
        if (fileout.is_open())
        {
            fileout.close();
        }
 
    }
    return 0;
}

Как передать значение веса архива в символьное число в документе и посчитать количество символов поставили меня в тупик. Если можно, то хотелось бы увидеть примерный код или как дописать свой.


Ответы (0 шт):