Saving to file

Hello, i have one problem which i cannot solve. I have class functions to read from a file bit by bit and "code" it, by inverting every third bit. I want to make a class function that compares the bits from original file and "coded" file and save the result into new file, 0 for different bits and 1 for equal bits. Thank you in advance.
closed account (zb0S216C)
Perhaps this:

1
2
3
4
5
6
7
8
9
10
11
// Read from the file, byte-by-byte...
std::ifstream File(...);
char Pos(NULL);

while(/*Not at the end of the file...*/)
{
    File.read((char*)Pos, 1);
    // Test the third bit.
    if(Pos &(1 << 2))
        // The third bit is 1. Do something...
}

Wazzak
Last edited on
thanks for the suggestion but it doesnt work for me. The source i have so far is this:

bool readBit ( bool *bit )
{
static unsigned char mask = 0x80;
static char byte;

if ( mask == 0x80 ) {
file.read( &byte, 1 );
if ( !file.good() )
return !OK;
}
*bit = ( byte & mask );
mask = ( mask == 0x01 )? 0x80 : mask >> 1;
return OK;

}

bool code (bool *bit)
{ static unsigned int k = 1;
if(k % 3 == 0 )
{ if (*bit == OK)
{ *bit=!OK;
k++;
return !OK;
}
else
{ *bit = OK;
k++;
return OK;
}
}
else
{ k++;
if (*bit == OK) return OK;
else return !OK;
}
}
this are the functions and the calling in main is like that
cout << "File (binary/coded):" << endl << "## BEGIN FILE ##" << endl;
bool status;
test.reopenSource();
while ( test.readBit(&bit) )
{ temp = bit;
status = test.decoder(&bit, test.code(&temp));
cout << temp ;
if ( !(++cont % 8) )
cout << " ";

}
cout << endl << "## END FILE ##" << endl;
if i try to make a function nothing happens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
bool readBit ( bool *bit )
{
	static unsigned char mask = 0x80;
	static char byte;

	if ( mask == 0x80 ) {
		file.read( &byte, 1 );
		if ( !file.good() )
			return !OK;
	}
	*bit = ( byte & mask );
	mask = ( mask == 0x01 )? 0x80 : mask >> 1;
	return OK;
}

bool code (bool *bit)
{	static unsigned int k = 1;
	if(k % 3 == 0 )
	{	if (*bit == OK)	
		{	*bit=!OK;
			k++;
			return !OK;
		}
		else 
		{	*bit = OK;
			k++;
			return OK;
		}
	}
	else 
	{	k++;
		if (*bit == OK) return OK;
		else return !OK;
	}
}

//this are the functions and the calling in main is like that 
cout << "File (binary/coded):" << endl << "## BEGIN FILE ##" << endl;
bool status;
test.reopenSource();
while ( test.readBit(&bit) ) 
{	temp = bit;
	status = test.decoder(&bit, test.code(&temp));
	cout << temp ;
	if ( !(++cont % 8) )
		cout << " ";

}
cout << endl << "## END FILE ##" << endl;
Your code is needlessly intricate. (obfuscated, you could say)

but it doesnt work for me
You need to be more specific.
if i try to make a function nothing happens
Something must happen ¿infinite loop or immediate termination?
¿And what are you changing in this situation?
Topic archived. No new replies allowed.