C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2004, 06:31 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
read a file bit-by-bit

Is there a way to read a file bit-by-bit?
what i want exactly is to read in 11 bits
at a time, manipulate the data based
on the 11 bits.

Note: the 11 is an arbitrary number.
That value may be any number 1 to 255.
Its more the function i am looking for, not
the number of bits.

thanks in advance.

Reply With Quote
  #2  
Old January 30th, 2004, 07:10 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
No -- the smallest unit that can be read or written is one BYTE -- 8 bits.

Reply With Quote
  #3  
Old January 30th, 2004, 07:36 PM
Kung Foo Master Kung Foo Master is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 68 Kung Foo Master User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
>one BYTE -- 8 bits
8 bits is an octet, a byte isn't necessarily 8 bits so you shouldn't rely on it.

Reply With Quote
  #4  
Old January 30th, 2004, 08:17 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
Most computer systems use 8-bit bytes, but you're right that one should not count on that.

http://whatis.techtarget.com/defini...i211721,00.html

Reply With Quote
  #5  
Old January 30th, 2004, 08:34 PM
madox madox is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Canada
Posts: 124 madox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 28 sec
Reputation Power: 10
fread() will let you specify the number of char's to read in (a char will typically be 8bits). You need to allocate storage space then pass in a pointer to the space. Just note that many small reads take the same or more time than one huge read.

I'm sure someone can post a istream way to do this, but I trust streams as far as I can throw them

Reply With Quote
  #6  
Old January 30th, 2004, 08:54 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
I don't like streams either -- formatting capability is really terrible compared to printf(). If you use VC++ 6.0 compiler and step into any of the stream functions you will find that they are really implemented by using FILE structure and associated functions. But those functions are a whole lot easier to use than streams. streams were very poorly designed in my opinion.

Reply With Quote
  #7  
Old January 30th, 2004, 09:37 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
ok

following on that theme
(not all chars are 8-bit bytes)
if there are differences how does the
fread -- or equivalent -- know how many
bits to travel to get to the next address?

if i knew this, it might be easier to
write a similar function
(or modify the existing one)
to jump any number of bits i declare.
yes?

so,
now for my next question.
does anyone know how this is accomplished?
(determining bit jumps, not overloading a function )

Reply With Quote
  #8  
Old January 30th, 2004, 09:52 PM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
I suggest you have a class that handles the file IO and buffers your bit requests. You can only manipulate in 8, 16, or 32 bit values (64 if you happen to have a 64 bit machine and OS) so you can't request 11 bits. You can request 16 and can mask off the other bits or code so that you ignore them. You will have to do some math to keep track of where your bit marker is and do bit shifting to get the alignment proper for each read, but it shouldn't be too difficult.

A word of caution: stick with unsigned integral values. Signed values behave differently when you do bit shifting and though a double will provide 8 64 bits of space, you may have problems with accessing them sometimes.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #9  
Old January 30th, 2004, 10:15 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
Quote:
Originally posted by L7Sqr
ok

following on that theme
(not all chars are 8-bit bytes)
if there are differences how does the
fread -- or equivalent -- know how many
bits to travel to get to the next address?


if i knew this, it might be easier to
write a similar function
(or modify the existing one)
to jump any number of bits i declare.
yes?

so,
now for my next question.
does anyone know how this is accomplished?
(determining bit jumps, not overloading a function )


you can only do that on computers that allow it -- unix, MS-DOS and Windows are all 8-bit operating systems. There are, of course, hundreds of other operating systems, and some of them may not use the 8-bit scheme. On those, I suspece fread() operates the same because the compiler manufacturer implemented it according to the operating system specs.

In unix, lines, ms-dos and all versions of Windows, there is no way to skip a specified number of bits -- that's impossible.

Reply With Quote
  #10  
Old January 31st, 2004, 12:47 AM
grumpy's Avatar
grumpy grumpy is offline
Left due to despotic ad-min
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Posts: 1,044 grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 12
Use caching. The standard mechanisms for reading a file read data a byte at a time, or in groups of multiple bytes.

The following UNTESTED example assumes bytes are 8 bits. It should be enough to get you started.

PHP Code:
 typedef struct _BitData
{
    
FILE *file;
    
int last_byte;
    
int bit_count;
BitData;

/*   The following assumes the argument is initialised with a
    valid FILE handle (eg returned from fopen), with bit_count
     set to EOF.  The function returns EOF if EOF of the file has
     been read.
*/

int GetBit(BitData *f)
{
       
int temp;
       if (
f->bit_count == 0)
       {
            
f->last_byte getc(f->file);
       }
       if (
f->last_byte == EOF) return EOF;

       
temp = (f->last_byte & (<< (f->bit_count)) != 0;   /* get next bit */
       
++f->bit_count;
       if (
f->bit_count == 7f->bit_count 0;
       return 
temp



If you think about it, the standard file read functions do this on a larger scale. Internally, they read blocks (eg 512 or more) bytes from a file, and then return one byte at a time from the block they have read.

Reply With Quote
  #11  
Old January 31st, 2004, 09:21 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
thank you.
most helpful.
this is more or less what i was after.
(although it would have been nice
to have a built-in function )
thanks for the help all.

Reply With Quote
  #12  
Old January 31st, 2004, 05:37 PM
grumpy's Avatar
grumpy grumpy is offline
Left due to despotic ad-min
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2003
Posts: 1,044 grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level)grumpy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 53 m 47 sec
Reputation Power: 12
It might have been nice to have had a built in function, but there is no such beast.

Wanting data a bit at a time is not the most common usage, whereas reading a byte (or a group of bytes) is. So that's what the standard library supports. Heck, there need to be some things left for the humble programmer to do :-)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > read a file bit-by-bit

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap