Hi! I'm working on a compress algorithm. But I couldn't write the software because I don't know how to read and write a file as binary like 101011111111011001. How can I read the like this ?

Also I want to ask if I can write the GUI on Java and C for background processes ? Will it work on every OS ?

Thanks!

Do you mean reading a file that's composed of the character s '0' and '1'...because when you read/write a file the data is binary...

I want to read a file exactly how it is on my hdd . like this: 101010111111000101010 . I have to create a new file after reading by changing some 1 and 0s . I have to change them to compress the file.. For example (very simple example) the file is :
11111111110000000000
the compressed file is :
solving :5x1 5x0

the file is 10111010.
I have a big plan for that! But first off all I need to know how to read and write a file as binary...

WaltP you can't do that as opening the file as binary. I don't know why :(

Edited 6 Years Ago by miles.85: n/a

I think your problem stems for the fact that you can't read anything smaller than a byte on a Intel/AMD computer. To accomplish interrogations smaller than a byte, you must resort to bit masking or bit shifting...

I want to read a file exactly how it is on my hdd . like this: 101010111111000101010 . I have to create a new file after reading by changing some 1 and 0s . I have to change them to compress the file.. For example (very simple example) the file is :
11111111110000000000
the compressed file is :
solving :5x1 5x0

the file is 10111010.
I have a big plan for that! But first off all I need to know how to read and write a file as binary...

WaltP you can't do that as opening the file as binary. I don't know why :(

The only way to read a file as binary is to open the file in binary mode. If it doesn't work, you can't read the file. Period.

You are under the misconception that reading in binary reads a bit at a time. Not true. It read the same as anything else -- bytes from the file are moved into bytes ( char s) into your read buffer. And that is exactly how it is on the disk -- a series of bytes.

After reading, it's then your job to look at the values as bits.

Add to that, there is no bit type in C. The lowest you can deal with is a byte.

OK! I understand that I can't read a file exactly as I think. So I'm asking again but different : How can I do this process :
the file is:
11111111110000000000
the compressed file is 10111010. <----5x1 5x0

How I can I do that ? It should be a way to do that. If it not possible on C, I know little bit Java maybe I can do it on Java ?!

OK! I understand that I can't read a file exactly as I think. So I'm asking again but different : How can I do this process :
the file is:
11111111110000000000
the compressed file is 10111010. <----5x1 5x0

How I can I do that ? It should be a way to do that. If it not possible on C, I know little bit Java maybe I can do it on Java ?!

Is the file a bunch of text characters 1 and 0? Or is it in fact a bunch of bytes 0xFF 0xA0 0x00?

We don't know what "the file is" really describes.

and how do you know 10111010 is 5x1 5x0 and not 0Bx1 1x0 or 2x1 3x0 1x0?

Now I'm going to explain that I want to do. I have a algorithm think. It works for every file type because it can compress the files as binary (as bit mode). That's why I gave you the 5x1 5x0 example. I am not a good programmer, I just think the algorithm and I will try it. But I don't know how can read the file as I want.
Think a txt file and it has writed inside ABC. I know that this is saving on hdd as (for example I don't know the bit code exactly) A=1000000, B=1100000, C=1110000. So the file is on my hdd 100000011000001110000 Am I right ? So I tell you that I can compress them. But I don't know how to work (read and write) with bits...

(This is something easy but I can't explain it exactly :( sorry for my English and Thanks for you interest.)

I can explain it in another way: When we open a files binary mode , we can read int, char (also bye on Java) ... When we read a integer, it reads 16 bits, when we read char, it reads 32 bit... But I want to read 1 bit.

I can explain it in another way: When we open a files binary mode , we can read int, char (also bye on Java) ... When we read a integer, it reads 16 bits, when we read char, it reads 32 bit... But I want to read 1 bit.

You can't read/address 1 bit, you have to use bit masking or bit shifting to interrogate anything smaller than 1 byte...its a hardware limitation...

Here's an example that will demonstrate why you can't address bits in Intel/AMD. Run the program below and note the addresses of the characters in the string "Hello, World!\n". You'll note they differ by one which is the smallest unit that can be addressed which just happens to be one character. The result of this? We have to use bit operations to mask out individual bits because the CPU doesn't understand the concept of address + 1/8 or address + 1/4.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char ch[] = "Hello, World!\n";

int main(int argc, char**argv)
{
	int i = 0, len = strlen(ch);

	for (i = 0; i < len; ++i)
	{
		fprintf(stdout, "ch[%d] has the value of %c and address->%p\n", i, ch[i], (void*)&ch[i]);
	}
	exit(EXIT_SUCCESS);
}
This article has been dead for over six months. Start a new discussion instead.