0

I need to read 256 bit for each step until binary file ends.Is there any operation in c to read bit by bit? I use fread function and read 32 chars. For all chars i shift bit by bit for 8 times. After reading ı write this 256 bits to a file. Do ı have to same thing to write? I mean do ı write 32 chars => 32*8 = 256 bit.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1334254
  • 39
  • 3
  • 8
  • you can write byte-by-byte. But disk drives are the slowest in computers so you'll want to write a bunch at once to speed up. Small files often aren't a problem but when you're writing a large amount of data then this should be taken into consideration – phuclv Jan 23 '14 at 08:33

1 Answers1

3

No, the minimum item you can read or write is a char (and keep in mind that's not necessarily 8 bits, it depends on the implementation). If you want to manipulate parts of a char once you have it in memory, you'll need to use the bitwise operators sich as &, | << and >> (and, or and left/right shift).

And yes, you can do fwrite to write an arbitrary number of characters (in the same manner as you use fread to read them).

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I have one more question :D I have a 32 char array so it is 256 bits. I need to rotate the left. IF it is 000111 it should be 001110. How can ı do that? Because ı don't have the bits, ı just have char array. – user1334254 May 04 '13 at 08:44