Solved

Accessing file at bit level?

Posted on 2002-05-02
5
130 Views
Last Modified: 2013-11-20
I want to access file at bit level such as
How many bit 0 are there in file?
How many bit 1 are there in file?
How many bit segment (such as 0,00,000,0000,1,11,111,1111...) are there in file?
Thank for your helping.
0
Comment
Question by:danh_phan
  • 2
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 6988068
Hi danh_phan,

here you can find a function which counts number of set bits in a long:
http://www.snippets.org/snippets/portable/BITCNT_1+C.php3

here you can find source how to use an array of chars as bit-array so
you can simply loop through the bits and count such segments:
http://www.snippets.org/snippets/portable/BITARRAY+C.php3

hope that helps,

ZOPPO
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6989516
The trick is to write a tool function that makes it easy to do a variety of things.  Then you can use existing tools to work out the rest of the task.

Here is a Function that converts the entire contents of a file into a series of '1's and '0's and places the output into a CString:

void ConvertFileTo1sAnd0s( LPCSTR sFilename, CString& sResult )
{
     CFile cFile( sFilename, CFile::modeRead );
     int nFileLen= cFile.GetLength();
     sResult= "";
     for (int j=0; j< nFileLen; j++ ) {
          BYTE c;
          char sz8bits[17];
          cFile.Read( &c, 1 );
          itoa( c, sz8bits, 2 );
          CString sTmp= sz8bits;
          while (sTmp.GetLength() < 8 ) {
               sTmp.Insert(0,'0');
          }
          sResult += sTmp;
     }
}
=-=-=-=-=-=-=-=-=-=-=-=-
Use it like this:

     CString s1sAnd0s;
     ConvertFileTo1sAnd0s( "c:\\temp\\test.bin", s1sAnd0s );

Then you can use CString member functions like:

int nOffset= s1sAnd0s.Find( "00000000000" );
if (nOffset == -1 ) {
     MessageBox("No strings of eleven 0's were found");
}

=-=-=-=-=-=-

-- Dan
0
 

Author Comment

by:danh_phan
ID: 6990554
How's about large file?
I think all source code here will not work properly if there are large files (~MB).
I think you can not convert large file to CString.
Thanks for your helping.

0
 

Author Comment

by:danh_phan
ID: 6990932
How's about large file?
I think all source code here will not work properly if there are large files (~MB).
I think you can not convert large file to CString.
Thanks for your helping.
I already this program, but it takes about 30 seconds for doing it (count all bit1 in 2MB file)
Thanks
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 6991872
>>I think you can not convert large file to CString.

Sure you can.  But once it gets up around about 1/2 the size of the avialable memory, you will run into serious performance problems as the O/S begins swapping memory to disk.

I provided a simple (100-point) solution that will work for most situations.  I have presumed that you could use the source code of that tool as a starting point.  

It would be very easy to write a version of that tool that reads the file in smaller chunks and looks through those chunks for the target string.  Let that be your assignment.  I think it will be FUN for you!

-- Dan
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unable to delete all specified values regedit 38 420
pre4 challenge 19 100
mapBully challenge 6 135
Bartender label printing - switch on and off graphics 3 40
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question