GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Nov-2009, 11:15
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 29
sl02ggp is on a distinguished road

Write a C++ program to compress a C++ source file


Compression and Decompression

As I have learned in the C++ source file, the lines have to start with many leading spaces. To save spaces in the saving file (to compress the file), the leading spaces may be counted and store the count as an integer in the beginning of the line followed by the rest of the characters of the line.

Example: Assume two ASCII characters are reserved for the integer count.

Source file: source.txt
No leading spaces on line 1
Five leading spaces on line 2
Fourteen leading spaces on line 3
Two leading spaces on line 4

Compressed file: source.zip

0No leading space on line 1
5Five leading spaces on line 2
14Fourteen leading spaces on line 3
2Two leading spaces on line 4

Write a C++ program to compress a C++ source file, store the compressed file in source.zip, and output to the monitor screen the number of space characters eliminated from the original source file. Assuming that the number of spaces are less than 99, reserve two ASCII characters to store the count.

Write a C++ program to uncompress the zip. File previously compressed and store the uncompressed file in source.rcv

Note: source.txt and source.rcv must look the same.
  #2  
Old 19-Nov-2009, 12:01
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need Assistance ASAP! (how to write this program code..lost where to start)


Where to start:

Code:
1) Open the file for reading (input). 2) Open (create) a new file for writing (output). 3) Read a line from input into a string. 4) Count the number of spaces at the beginning of the string and remove them. 5) Write that number (as a 2-digit number) to the output file. 6) Write the rest of the string to the output file. 7) Repeat steps 3-6 until you have processed every line in the file 8) Close both files.
  #3  
Old 19-Nov-2009, 12:07
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 29
sl02ggp is on a distinguished road

Re: Need Assistance ASAP! (how to write this program code..lost where to start)


can you show me some C++ code, please?
  #4  
Old 19-Nov-2009, 12:56
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 29
sl02ggp is on a distinguished road

Re: Need Assistance ASAP! (how to write this program code..lost where to start)


this is what i got:

CPP / C++ / C Code:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	char c;
	char M[1000];
	int cnt=0;

	ifstream infile;
	infile.open("file.txt");   //file to be encrypted
	ofstream outfile;
	outfile.open("file.zip");  //encrypted file

	while ( infile.get(c))	// read all characters in the file into array M
	{
		M[cnt] = c;
		cnt++;
	}

	for (int i=cnt-1; i >= 0; i--)	// reverse the order of characters and store them
	{								// in the encrypted file.
		outfile << M[i];
	}
}
    

  #5  
Old 19-Nov-2009, 14:38
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 761
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need Assistance ASAP! (how to write this program code..lost where to start)


Instead of reading the entire file into an array, try reading one line at a time with the getline() method.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	char line[1000];

	ifstream infile;
	infile.open("file.txt");   //file to be encrypted
	ofstream outfile;
	outfile.open("file.zip");  //encrypted file

	while ( infile.good())	
	{
		// Read a line from the file
		infile.getline(line, 1000);

		// Count the leading spaces
		int leadSpaces = CountLeadingSpaces(line);

		// Write the number of leading spaces to the output file as 2 digits
		
		// Write the rest of the line to the output file
		outfile << (line + leadSpaces);
	}
	
	infile.close();
	outfile.close();
}
    

  #6  
Old 20-Nov-2009, 07:14
TanLorik TanLorik is offline
Junior Member
 
Join Date: Nov 2009
Posts: 38
TanLorik has a spectacular aura aboutTanLorik has a spectacular aura about

Re: Write a C++ program to compress a C++ source file


compiled and tested
post if you need help making the decompression programm too

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;
int CountLeadingSpaces(char* );

int main()
{
	ifstream infile;
	infile.open("file.txt");   //file to be encrypted
	ofstream outfile;
	outfile.open("file.zip");  //encrypted file

	while ( infile.good())	
	{
		char line[1000];
		// Read a line from the file
		infile.getline(line, 1000);

		// Count the leading spaces
		int leadSpaces = CountLeadingSpaces(line);

		// Write the number of leading spaces to the output file as 2 digits
		
		// Write the rest of the line to the output file
		outfile << leadSpaces << line << endl;
	}
	
	infile.close();
	outfile.close();

}	
int CountLeadingSpaces(char* line)
{
	int Spaces=0;
	while(line[0] == 32 || line[0] == '\t')
	{
		if(line[0]=='\t')Spaces+=4;
		else Spaces+=1;
		strcpy(line, &line[1]);
	}

	return Spaces;
}


although for some reason it won't work on MSVC++ because of the stupid strcpy_s, my program keeps ending halfway.

CPP / C++ / C Code:
strcpy(line, strlen(line), &line[1])
  #7  
Old 21-Nov-2009, 02:44
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 349
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Write a C++ program to compress a C++ source file


Quote:
Originally Posted by TanLorik
compiled and tested
post if you need help making the decompression programm too

Quote:
Note: source.txt and source.rcv must look the same.
CPP / C++ / C Code:
		if(line[0]=='\t')Spaces+=4;
Converting a tab into spaces is a fatal flaw. There's no way you could ever convert the file back into the original anymore.

What happens with your program if the line length is, say, 1021 characters?
  #8  
Old 21-Nov-2009, 04:42
TanLorik TanLorik is offline
Junior Member
 
Join Date: Nov 2009
Posts: 38
TanLorik has a spectacular aura aboutTanLorik has a spectacular aura about

Re: Write a C++ program to compress a C++ source file


well, it just said "look" the same, not be the same. The other option would be to store 2 separata variables, 1 for spaces, one for tabs, or to just forget about the tabs and only do the spaces. Witchever way you pick, the result will be both visually, and for the compiler the same whan you compare source.txt to source.rcv


As for the spaces-> tabs conversion, this turns it back to the "way it was".

CPP / C++ / C Code:
int spaces,i;
ifstream inFile("source.zip");
ofstream outFile("source.rcv");
while(!inFile.eof())
    {inFile.getlinine(line, 1000);
    if( '0' <= line[1] <= '9')
        {spaces = int(line[0])*10 + int(line[1]);
        for(i=1;i <= spaces/4; i++) outFile << '\t';
        for(i=1;i <= spaces%4;i++) inFile << ' ';
        outfile << &line[2];}
    else
        { spaces = int(line[0]);
        for(i=1;i <= spaces/4; i++) outFile << '\t';
        for(i=1;i <= spaces%4;i++) inFile << ' ';
        outfile << &line[1];}}
inFile.close();
outFile.close();
// haven't tested it, just wrote it at the spur of the moment



as for the 999+ characters, I was just continuing the code wrote by fakepoo, if you want I can dinamically alocate memory depending on strlen bla bla..., but these scenarios generally cover theoretical situations, just make it char line [100000];.
  #9  
Old 21-Nov-2009, 06:24
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 349
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Write a C++ program to compress a C++ source file


Quote:
Originally Posted by TanLorik
well, it just said "look" the same, not be the same.
A tab is not four spaces. A tab is just a tab. In every editor I use to write code there is an option for "tab length", that is, how many spaces should one tab expand to (when ticking the "convert tabs into spaces" option). I can set it to 40 or I can set it to 2. Would my source file then look the same if compressed and decompressed with your method?

Quote:
if you want I can dinamically alocate memory depending on strlen bla bla..., but these scenarios generally cover theoretical situations, just make it char line [100000];.
I was just pointing out what I think are design flaws, there's nothing I want with this program.

So, considering a line MOSTLY would be around 50-100 characters long, it's okay for you to waste over 99 percent of the allocated space most of the time? And allocate 100000 chars on stack?

A line over 999 characters is not a theoretical situation. You see such lines when examining the source code of web pages. One page I quickly looked up was 21000 characters long.
  #10  
Old 21-Nov-2009, 07:59
TanLorik TanLorik is offline
Junior Member
 
Join Date: Nov 2009
Posts: 38
TanLorik has a spectacular aura aboutTanLorik has a spectacular aura about

Re: Write a C++ program to compress a C++ source file


Quote:
Originally Posted by sl02ggp
Compression and Decompression
As I have learned in the C++ source file, the lines have to start with many leading spaces. To save spaces in the saving file (to compress the file), the leading spaces may be counted and store the count as an integer in the beginning of the line followed by the rest of the characters of the line.

criticism noted, and also it looks like I deviated from the original request, I just had to remove spaces, nothing about the tabs

although, how would you do the scenario where you had to remove both spaces and tabs?I mean if you have the following sequence

CPP / C++ / C Code:
"\t    \t\t\t     \t\t\   "


how would the end file look like?(note to not actually take more space than the original )
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help C++ program statquos C++ Forum 0 06-Jun-2008 08:47
Can you help me solving this program!plzz post the code of this program plzz.. albert_123 C Programming Language 2 12-Sep-2006 06:47
Urgent ! Pls Help Me ! mycashmoney C Programming Language 4 01-Jul-2006 22:49
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:35.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.