hi,
I have a program that compresses a file but for some reason my output file comes up empty every single time ...I dont get it . can some body tell me what i might be overlooking
The sample input file looks like this
..GGGGGG...OOOOOOOO...OOOOOOOO...DDDDDD
and my function has to compress that file so it looks like
2.6G3.8O3.6D
this is what my code looks like
//****************************************
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
//functions prototypes
void menu();
//void compress();
void compress( ifstream &inFile, ofstream &outFile );
void decompress();
string input();
string output();
int integer ();
int main()
{
ifstream inFile;
ofstream outFile;
string input_file, output_file;
int option;
do
{
menu();
option = integer();
//input_file = input ();
//output_file = output();
if ((option == 1) || (option == 2))
{
input_file = input ();
//inFile.open(input_file.c_str());
if (!inFile)
{
cout << "Error! Cannot open file!" << endl;
}
else if (option == 1)
{
//outFile.open(output_file.c_str());
output_file = output();
//compress();
compress( inFile, outFile);
}
else if (option == 2)
{
//outFile.open(output_file.c_str());
output_file = output();
decompress();
//decompress( ifstream & input_file, ofstream & output_file );
}
else
cout << "Error. Terminating program now!" << endl;
}
else
cout << "Invalid integer entered" << endl;
}
while (option != 0);
inFile.close();
inFile.clear();
outFile.close();
outFile.clear();
return 0;
}
void menu ()
{
cout << "******** Compression Menu ********" << endl;
cout << "0. Exit Program" << endl;
cout << "1. Compress File" << endl;
cout << "2. Decompress File" << endl;
cout << "**********************************" << endl;
}
int integer ()
{
bool flag;
int choice;
cout << "Input your selection now:" << endl;
cin >> choice;
cout << choice << endl;
if ((choice == 0) || (choice == 1) || (choice ==2))
flag = true;
else
flag = false;
while (flag !=true)
{
cin.clear();
cin.ignore(200,'\n');
cout << "Invalid character entered!!" << endl;
cout << "Please enter an integer value:" << endl;
cin >> choice;
cout << choice << endl;
}
cin.ignore(200, '\n');
return choice;
}
string input()
{
string input1;
ifstream inFile;
cout << "Enter in the name of the input file now:" << endl;
cin >> input1;
cout << input1 << endl;
inFile.open(input1.c_str());
while (!inFile)
{
cout << "Error: unable to open file!" << endl;
inFile.clear();
cout << "Enter in the name of the input file now:" << endl;
cin >> input1;
cout << input1 << endl;
inFile.open(input1.c_str());
}
return input1;
}
string output()
{
ofstream outFile;
string output1;
cout << "Enter in the name of the output file now:" << endl;
cin >> output1;
cout << output1 << endl;
outFile.open(output1.c_str());
return output1;
}
void compress( ifstream& inFile, ofstream& outFile )
{
char n;
int Count=0;
char temp[256];
int num_char = 0;
while (inFile >> n) {
for(int i=0; i<256; i++){
temp[i]= n; }
}
for(int j=0; j<256; j++){
if(temp[j]=temp[j+1]){
Count = Count + 1;}
else{
outFile << "i'm here"<<endl;
outFile << Count << n ;
outFile<< endl;
}
}
}