Click here to Skip to main content
15,883,921 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Class or object? Pin
Albert Holguin26-Jun-15 10:26
professionalAlbert Holguin26-Jun-15 10:26 
AnswerRe: Class or object? Pin
Albert Holguin26-Jun-15 10:30
professionalAlbert Holguin26-Jun-15 10:30 
AnswerRe: Class or object? Pin
Stefan_Lang30-Jun-15 23:50
Stefan_Lang30-Jun-15 23:50 
GeneralRe: Class or object? Pin
_Flaviu1-Jul-15 22:17
_Flaviu1-Jul-15 22:17 
GeneralRe: Class or object? Pin
Stefan_Lang1-Jul-15 22:36
Stefan_Lang1-Jul-15 22:36 
GeneralRe: Class or object? Pin
_Flaviu2-Jul-15 1:21
_Flaviu2-Jul-15 1:21 
AnswerRe: Class or object? Pin
Kevin McFarlane6-Jul-15 23:16
Kevin McFarlane6-Jul-15 23:16 
QuestionVery simple binary file compressor in C Pin
stonemanhero26-Jun-15 1:52
stonemanhero26-Jun-15 1:52 
Hello as subject says i am trying to make some simple binary file compressor (for now). I started with basics.
This code opens file in binary mode reads byte by byte and input bytes writes to output "compressed" file. If byte == 0x00 counts number of 0x00's and writes 0x00 and number of 0x00's.

C++
#include<stdio.h>
#include<stdlib.h>

int main()
{
	int i, duzina, stanje=0, brojac=0;
	unsigned char chr;
	FILE* ulaz = fopen("../testapp/testapp", "rb");
	FILE* izlaz = fopen("../testapp/testapp.zip", "wb");
	
	if(ulaz == NULL)
	{
		printf("Problem with file!\n");
		exit(1);
	}
	if(izlaz == NULL)
	{
		printf("Problem with output file!\n");
		exit(1);
	}

	fseek(ulaz, 0, SEEK_END);
	duzina = ftell(ulaz);
	fseek(ulaz, 0, SEEK_SET);

	for(i=0; i<duzina; i++)
	{
		chr = fgetc(ulaz);
		if(chr == 0x00) 
		{
			if(stanje == 0)
			{
				stanje=1;
				brojac=1;
			}
			else
				brojac++;
		}
		else
		{
			if(stanje == 0)
				fputc(chr, izlaz);
			else
			{
				fputc(0x00, izlaz);
				fwrite(&brojac, sizeof(int),1,izlaz);
				fputc(chr, izlaz);
				printf("%d ", brojac);
				stanje=0;
				brojac=0;
			}
		}
	}

	fclose(izlaz);
	fclose(ulaz);
}




To "decompress" file i use this code which as input have "compressed" file and read byte by byte if byte == 0x00 reads integer and write N times 0x00 (N value = readed integer)

C++
#include<stdio.h>
#include<stdlib.h>

int main()
{
	int i,j, duzina, brojac=0;
	unsigned char chr;

	FILE* ulaz = fopen("../testapp/testapp.zip", "rb");
	FILE* izlaz = fopen("../testapp/testapp1", "wb");
	
	if(ulaz == NULL)
	{
		printf("Problem with file!\n");
		exit(1);
	}
	if(izlaz == NULL)
	{
		printf("Problem with output file!\n");
		exit(1);
	}

	fseek(ulaz, 0, SEEK_END);
	duzina = ftell(ulaz);
	fseek(ulaz, 0, SEEK_SET);

	for(i=0; i<duzina; i++)
	{
		chr = fgetc(ulaz);
		if(chr == 0x00)
		{
			fread(&brojac, sizeof(int), 1, ulaz);
			printf("%d ", brojac);
			for(j=0; j<brojac; j++)
				fputc(0x00, izlaz);
			i+=4;	
		}
		else
			fputc(chr, izlaz);
	}

	fclose(izlaz);
	fclose(ulaz);
}



So my problem is that when "decompressing" is finshed some bytes are missing (program works correct).

Byte comparing:

[stone@hero testapp]$ diff testapp1.hex testapp.hex 
443,444c443,444
< 0001c00 0001                                   
< 0001c01
---
> 0001c00 0001 0000 0000 0000 0000 0000 0000 0000
> 0001c10
[stone@hero testapp]$


I'm on: Linux hero 3.18.15-1-MANJARO #1 SMP PREEMPT Sun Jun 14 10:09:07 UTC 2015 x86_64 GNU/Linux if it matters.

Source code for test app:
C++
#include<stdio.h>
#include<time.h>

main()
{
	srand(time(NULL));
	printf("\nHello, stone... ");
	printf("Your random number is: %d\n\n", rand() % 100 + 1);
}



What can be a problem with missing bytes?
AnswerRe: Very simple binary file compressor in C Pin
CPallini26-Jun-15 3:00
mveCPallini26-Jun-15 3:00 
QuestionCreating a standalone exe with Visual Studio 2013 native unit tests? Pin
Joe Woodbury25-Jun-15 10:53
professionalJoe Woodbury25-Jun-15 10:53 
SuggestionRe: Creating a standalone exe with Visual Studio 2013 native unit tests? Pin
Albert Holguin26-Jun-15 10:40
professionalAlbert Holguin26-Jun-15 10:40 
GeneralRe: Creating a standalone exe with Visual Studio 2013 native unit tests? Pin
Joe Woodbury26-Jun-15 11:15
professionalJoe Woodbury26-Jun-15 11:15 
QuestionCan resource statement CTEXT be used with CStatic Pin
ForNow24-Jun-15 9:09
ForNow24-Jun-15 9:09 
AnswerRe: Can resource statement CTEXT be used with CStatic Pin
CPallini24-Jun-15 12:09
mveCPallini24-Jun-15 12:09 
GeneralRe: Can resource statement CTEXT be used with CStatic Pin
ForNow24-Jun-15 12:44
ForNow24-Jun-15 12:44 
QuestionWindows 8.1 Event log problem Pin
zeki yugnak24-Jun-15 3:32
zeki yugnak24-Jun-15 3:32 
QuestionRe: Windows 8.1 Event log problem Pin
David Crow24-Jun-15 3:46
David Crow24-Jun-15 3:46 
AnswerRe: Windows 8.1 Event log problem Pin
zeki yugnak24-Jun-15 4:16
zeki yugnak24-Jun-15 4:16 
QuestionHow to best pull together data from multiple sources (hardware sensors) in Win32? Pin
MW-83121724-Jun-15 2:29
MW-83121724-Jun-15 2:29 
AnswerRe: How to best pull together data from multiple sources (hardware sensors) in Win32? Pin
CPallini24-Jun-15 2:37
mveCPallini24-Jun-15 2:37 
GeneralRe: How to best pull together data from multiple sources (hardware sensors) in Win32? Pin
MW-83121724-Jun-15 3:08
MW-83121724-Jun-15 3:08 
GeneralRe: How to best pull together data from multiple sources (hardware sensors) in Win32? Pin
CPallini24-Jun-15 3:18
mveCPallini24-Jun-15 3:18 
GeneralRe: How to best pull together data from multiple sources (hardware sensors) in Win32? Pin
Albert Holguin24-Jun-15 11:57
professionalAlbert Holguin24-Jun-15 11:57 
GeneralRe: How to best pull together data from multiple sources (hardware sensors) in Win32? Pin
CPallini24-Jun-15 12:11
mveCPallini24-Jun-15 12:11 
GeneralRe: How to best pull together data from multiple sources (hardware sensors) in Win32? Pin
Albert Holguin24-Jun-15 12:16
professionalAlbert Holguin24-Jun-15 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.