How to use ofstream in this certain methodology

  c++, c++11, c++14, ofstream

I believe the code here is correct. It does behave mostly as expected. But unfortunately, it will not write to file. I do not understand why, though. There isn’t a reason for it to skip this step. I am wondering where I have missed in my syntax. If anyone can show me the reason, I’d be most appreciative. I love this site. BTW, this is compression.

#include <fstream>
#include <iostream>
#include <bitset>
#include <vector>
#include <sstream>
#include <string.h>
#include <chrono>
#include <cmath>

using namespace std;

const int MAX_SEG_CNT = 1000000;

int main(int argc, char **argv)
{

    string ghost = "", boo = "", agh = "";
    stringstream fd;
    ifstream fin(argv[2]);
    if (!fin)
        exit(0);
    ofstream fout(argv[3], std::ios_base::out | std::ios_base::binary);
    if (!fout)
        exit(0);
    fd << fin.rdbuf();
    ghost = fd.str();
    fd.str("");

    uint64_t file_size = ghost.length();

    auto start = std::chrono::system_clock::now();
    // Start Timer
    std::time_t start_time = std::chrono::system_clock::to_time_t(start);
    cout << std::ctime(&start_time) << flush;

    cout << "Compressing file... (this could be awhile)" << flush;
    size_t fg = 0;
    // Iterator count through file
    uint64_t H = 0, G = 0;
    bool p = 0;
    uint64_t PERCENT = 0, PER_FIX = 0, OGRE = 0, FIEND = 0, OUTPUT_BYTE_CNT = 0;
    agh = ghost;
    if (ghost.length() > MAX_SEG_CNT)
        ghost = ghost.substr(0, MAX_SEG_CNT % ghost.length());
    bitset<64> total = 0 /* iterates file */, pumpkinbomb = 0 /* compressed cypher */;
    if (strcmp(argv[1], "-c") == 0)
    {
        uint8_t zombie = 0;
        string skeleton = "";
        while (ghost.length() > 0)
        {
            uint8_t i = 0;
            string temp_ghost = (ghost.length() >= 8) ? ghost.substr(0, 8) : ghost;
            for (uint8_t y : temp_ghost)
            {
                total = total.to_ullong() + (y << (i * 8));
                i++;
            }

            FIEND = OGRE;
            
            // Count number of tiers in our pyramid
            // Cut total to 4 bits long
            // via our algorithm of linear XOR
            while (total.to_ullong() >= 1)
            {
                // MAX_STEPS_INT is the amount of rows
                // to let pass by before making another
                // point of entry to the pyramid of numbers.
                pumpkinbomb = total[0] + (pumpkinbomb.to_ullong() << 1);
                OGRE++;
                uint8_t f = 0;
                uint64_t bats = 0;
                while (total.to_ullong() >= 2)
                {
                    bats += ((total[0] ^ total[1]) << f);
                    total >>= 1;
                    f++;
                }
                total = bats;
                if ((total.to_ullong() >> 1) <= 1)
                    break;
            }
            skeleton.push_back((char)(OGRE - FIEND));
            if (OGRE > 62)
                cout << "." << flush;
            zombie += OGRE;
            while (zombie > 50 && pumpkinbomb.to_ullong() > 0)
            {
                uint8_t x = pumpkinbomb.to_ullong() % 256;
                (x == 0) ? boo.push_back('{$content}') : boo.push_back((char)(x));
                pumpkinbomb >>= 8;
            }
            if (boo.length() > 0)
            {
                boo = skeleton + '$' + boo;
                boo.insert(boo.begin(),'&');
                OUTPUT_BYTE_CNT += boo.length();
                fout << boo;
                boo.clear();
                skeleton.clear();
                zombie = OGRE = 0;
            }
            if (ghost.length() <= 8 && agh.length() == 0)
                break;
            if (ghost.length() <= 8 && agh.length() >= MAX_SEG_CNT)
            {
                ghost = agh.substr(0, MAX_SEG_CNT);
                agh = agh.substr(MAX_SEG_CNT);
            }
            else if (ghost.length() <= 8 && agh.length() < MAX_SEG_CNT)
            {
                ghost = agh;
                agh.clear();
            }
            ghost = ghost.substr(temp_ghost.length());
            H++;
            if (PER_FIX < 99 && p == 0 && agh.length() + 1 < file_size - ((file_size / 100) * PER_FIX))
            {
                while (PERCENT < 99 && (file_size / 100) * PERCENT < (file_size - agh.length()))
                    PERCENT++;
                PER_FIX = PERCENT;
                double PRCNT = (double)((OUTPUT_BYTE_CNT) / ((double)(file_size - agh.length())));
                
                cout << (PER_FIX) << "/" << round(PRCNT * 100) << "% .." << flush;
                p = 1;
            }
            else if (p)
            {
                p = 0;
            }
        }
        while (pumpkinbomb.to_ullong() > 0)
        {
            uint8_t x = pumpkinbomb.to_ullong() % 256;
            (x == 0) ? boo.push_back('{$content}') : boo.push_back((char)(x));
            pumpkinbomb >>= 8;
        }
        if (boo.length() > 0)
        {
            boo = skeleton + '$' + boo;
            boo.insert(boo.begin(),'&');
            OUTPUT_BYTE_CNT += boo.length();
            fout << boo;
            boo.clear();
            skeleton.clear();
        }
        
    }
    else if (strcmp(argv[1], "-d") == 0)
    {
        do
        {
            vector<uint64_t> hobo {};
            string skeleton = ghost.substr(1,ghost.find("$",1));
            string boo_ghost = ghost.substr(ghost.find("&",skeleton.length()));
            if (ghost.find("&",skeleton.length()) == std::string::npos)
                ghost.clear();
            else
                ghost = ghost.substr(1 /*&*/ + skeleton.length() + 1 /*$*/ + boo_ghost.length());

            bitset<64> troll = 0;
            for (uint8_t x : boo_ghost)
            {
                troll = troll.to_ullong() + x;
            }
            
            for (int i = 0 ; i < skeleton.length() ; i++)
            {
                uint8_t bones = (int)(skeleton[i]);
                bool clown = troll[(int)(skeleton[i]) - bones];
                uint64_t house = (int)(clown);
                bitset<64> ghoulish = (int)troll[0];
                while (bones > 0)
                {
                    house <<= 1;
                    house += (int)(clown);
                    uint8_t j = (int)(skeleton[i]) - bones, h = 0;
                    while (h < j)
                    {
                        clown = ghoulish[h] ^ clown;
                        house <<= 1;
                        house += (int)(clown);
                        h++;
                    }
                    ghoulish = house;
                    bones--;
                    clown = troll[(int)(skeleton[i]) - bones];
                }
                hobo.push_back(house);
            }
            for (uint64_t x : hobo)
            {
                uint64_t y = x;
                string t = "";
                while (y > 0)
                {
                    uint8_t v = y%256;
                    t.push_back((char)(v));
                    y >>= 8;
                }
                ///////// Here's the output line I need to work.
                fout << t;
            }
        } while (ghost.find("&") != std::string::npos);
    }
    // End process
    auto end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end - start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);

    std::cout << "nfinished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "sn";
    cout << "done." << endl
         << flush;

    exit(0);
}

Source: Windows Questions C++

LEAVE A COMMENT