| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Decompressing Arrays into a File
I need to decompress words in a compressed file into a new one. It must be a function and, pls try to level the skill level with what you see. thanks
#include<iomanip> //getline #include<cstdlib> #include<iostream> #include<string> //string #include<fstream> //ifstream and ofstream using namespace std; int search(ifstream& myfile, string name[], string temp); int sort(ifstream& myfile, string name[], int freq[]); void compress(ifstream& myfile, string name[], int freq[]); const int MAX = 50; //Max amount of data is 50 int main() { string name[MAX]; //arrays for the words int size, freq[0]; //arrays for frequency of words string FileName, OutFileName; //Filenames char cd; //compression or decompression check ifstream myfile; //file command for incoming file ofstream outfile; //file command for output file myfile.open("input.dat"); cout << "Do you want to compress or decompress? (C/D)? "; cin >> cd; switch(cd) { case 'C': case 'c': { sort(myfile, name, freq); compress(myfile, name, freq); } break; case 'D': case 'd': { //decompress } break; default: { cout << "Please enter C or D. " << endl; return 1; } } system("PAUSE"); return 0; } int search(string name[], string tmpword) { int ctr = 0; for(ctr = 0; ctr < MAX; ctr++) { if(name[ctr] == tmpword) { return ctr; } } return -1; } int sort(ifstream& myfile, string name[], int freq[]) { int product, temp; string tmpword, OutFileName, FileName; ofstream outfile, outfile2; int N = -1, F = -1, ctr = 0, size; cout << "What's the name of the full sized file? "; cin >> OutFileName; outfile.open(OutFileName.c_str()); cout << OutFileName <<".cmp " << "created." << endl; while(myfile && N < MAX) { getline(myfile, tmpword); product = search(name, tmpword); if(product != -1) { freq[product]++; } else { N++; F++; name[N] = tmpword; freq[F] = 1; } size = N + 1; } outfile << size << endl; for(temp = 0; temp < (N + 1); temp++) { outfile << temp << ". " << name[temp] << " frequency " << freq[temp] << endl; } myfile.clear(); myfile.close(); outfile.close(); } void compress(ifstream& myfile, string name[], int freq[]) { int product, temp; string tmpword, OutFileName, FileName; ofstream outfile; ofstream outfile2; int N = -1, F = -1, ctr = 0, size; myfile.open("input.dat"); outfile2.open("xylz.cmp"); while (myfile && N < MAX ) { getline(myfile, tmpword); product = search(name, tmpword); if(product != -1) // Word Found, Update Frequency { outfile2 << product << " "; } else { N++; name[N] = tmpword; F++; freq[F] = 1; } } myfile.close(); myfile.clear(); } |
|
#2
|
|||
|
|||
|
You can't just come here and dump your assignment code in the hopes that someone will do it for you.
If you have specific questions about something then ask, -KM- |
|
#3
|
|||
|
|||
|
I wasn't expecting somon to do the assignment, i was hoping somone would simply give me some insight on the decompression part of my problem, i already have the compression, arrays assigned, and func calls. Just needed a little hel on my decompression function.
|
|
#4
|
|||
|
|||
|
The decompression will depend on how it was compressed to start with. Perhaps if you provide us with some details.
-KM- |
|
#5
|
|||
|
|||
|
ok, thnks for any insight. I have a word list, no whitespace and only one word per line. I took the words and placed them into a word list of repeating words with their frequencies, which functions perfectly. My compression file is to display how many words there are, they only count once if they are repeated, each word once, then a row of numbers displaying each line of the file and the data it holds. Then i should be able to create a deecompressed file of the compressed file that will seperate the words from their table into the original file.
Here is an example output: Example Interaction with the user: Do you want to compress or decompress? (C/D) CWhat's the name of the full-size file? xyz1xyz1.cmp created Example Interaction with the user: Do you want to compress or decompress? (C/D) DWhat's the name of the compressed file? xyz1.cmpxyz1.cmp.decmp created Input file theumbrellathecatthecowtheblackcat The word list (numbers show position in the list) 0. the frequency 4 1. umbrella frequency 1 2. cat frequency 2 3. cow frequency 1 4. black frequency 1 The compressed (output) file 5theumbrellacatcowblack0 1 0 2 0 3 0 4 25thecatumbrellacowblack0 1 0 2 0 3 0 4 1 0 4 2 Decompressed Output file: thecattheumbrellathecowtheblackcattheblack umbrella//I'm also having some trouble getting the words to show up in my compressed file...sometimes it will work when i put a for loop in there, but its rare.-am0k |
|
#6
|
|||
|
|||
|
There's a fundamental flaw in what you are trying to do. Once you have compressed the information down into the number of occurances there is no way you will be able to get the ordering back. The best you will be able to do when decompressing is list the words out the number of times they occured but not in the same order.
-KM- |
|
#7
|
||||
|
||||
|
Perhaps using something similar to indexes?
For exmaple... Code:
If your wordlist is:
cat
dog
rabbit
cat
cat
dog
animal
Set up an index like:
0cat
1dog
2rabbit
3animal
Then the wordlist could become:
0120013
You also might benefit by using binary... in my example, what happens on the tenth word? ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Decompressing Arrays into a File |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|