Hi,

I have to read a file up to a fixed size, how can I do that?
Any help is appreciated.

example: I have a huge text file and I want to read 1000 bits at a time. How can I achieve this?

Thanks,

You can only read bytes, never bits :)

Open the file in binary mode then use file.read() to read into a buffer of 1000 bytes

unsigned char buf[1000];
ifstream in("filename",ios::binary);
in.read(buf,sizeof(buf));

1000 bits = 125 bytes

Therefore, to read 1000 bits, all you have to do is reading 125 bytes. Using the example given by Ancient Dragon, your code should looks like:

unsigned char buffer[125];
ifstream in("filename", ios::binary);   // provide extension to filename if exist
in.read(buffer, sizeof(buffer));

Be very careful when manipulating bits. Its not as easily handled as bytes. Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.