Example #1
0
void Tree::writefile(char *pathIn){
    ifstream file;
    ofstream write;
    string aux;
    string aux2;
    string temporaria;
    int sobra = 0;
    int sizefile = 0;
    BitArray *buffer = new BitArray();

    aux.clear();
    temporaria.clear();
    codificar(root, aux);
    aux.clear();

    file.open(pathIn , ios::in | ios::binary | ios::ate);

    if(file.is_open()){

        int size = file.tellg();
        file.seekg(0, ios::beg);
        int i;
        int iw =1;

        for(i=0; i< size; i++){
            unsigned char c = file.get();
            int j = c;
            aux += codeNodes[j];
            if(aux.length() >= 1024*8){
                sobra = aux.length() - 1024*8;
                temporaria = aux.substr(1024*8, sobra);
                for(int k = 0;k < (aux.length()/8); k++){
                    int n = k*8;
                    aux2 = aux.substr(n, 8);
                    codenode_to_bitarray(aux2, buffer);

                }
                iw++;
                write.open("temp.huff", ios::out | ios::binary | ios::app);
                if(write.is_open()){
                    for(int z = 0;z < aux.length()/8;z++){
                        write << buffer->getArray()[z];
                        sizefile++;
                    }
                }

                write.close();
                aux.clear();
                aux = temporaria;
                buffer->clear();
            }
        }
        int y = (aux.length()/8);
        sobra = aux.length() - (y*8);
        aux2 = aux.substr(y*8,sobra);
        string teste = aux2;


        for(int k = 0;k < (aux.length()/8); k++){
            int n = k*8;
            aux2 = aux.substr(n, 8);
            codenode_to_bitarray(aux2, buffer);

        }

        int u = 8 - sobra;
        sizeTrash = u;
        aux2 = teste;

        for(int i = 0;i < u;i++){
            aux2 += '0';
        }

        codenode_to_bitarray(aux2, buffer);

        sizeLast = (buffer->getCurrent() / 8);
        sizeLast--;

        write.open("temp.huff", ios::out | ios::binary | ios::app);

        if(write.is_open()){
            for(int z=0; z <= sizeLast;z++){
                write << buffer->getArray()[z];

                sizefile++;
            }
        }
        write.close();

        sizeTrash = buffer->trashLenght();


        buffer->clear();

    }
    else cout << "TRASH!in WriteFile" << endl;
    file.close();

}