📄 e2101.cpp
字号:
// =======================================================
// Chapter 21, Example 1
// RLE Compression
// =======================================================
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include "Array.h"
#include "RLE.h"
int GetFileSize( char* p_filename )
{
FILE* file = fopen( p_filename, "rb" );
unsigned long int size = 0;
// skip to the end of the file
fseek( file, 0, SEEK_END );
// retrieve the size
size = ftell( file );
// close the file
fclose( file );
// return the size.
return size;
}
void main()
{
Array<char> original( 1 );
Array<char> uncompressed( 1 );
RLE<char> compressed;
// filename
char filename[80];
// index
int index;
// get the file name
cout << "Enter file name: ";
cin >> filename;
// resize the array so that the file will fit.
original.Resize( GetFileSize( filename ) );
// read the file in
original.ReadFile( filename );
// compress the array into an RLE
compressed.Compress( original );
// save the RLE to disk
strcat( filename, ".rle" );
compressed.SaveData( filename );
// show statistics
cout << "Original File Size: " << compressed.m_size << endl;
cout << "Compressed File Size: " << compressed.m_runs * 2 << endl;
cout << "Compression Ratio: ";
cout << (float)(compressed.m_size) / (float)(compressed.m_runs * 2);
cout << endl;
// decompress the RLE into the second array.
compressed.Decompress( uncompressed );
// check to see if the arrays match
cout << "Checking Array Integrity..." << endl;
for( index = 0; index < compressed.m_size; index++ )
{
if( original[index] != uncompressed[index] )
{
cout << "ERROR, DECOMPRESSION UNSUCCESSFUL!!" << endl;
return;
}
}
cout << "Arrays match!" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -