Hi.

I want to read a file and scan the file to see if the file contains 0x000001 (24 bits, 23 zero and 1 one bit). if file contains 0x000001, return a "yes", else return a "no".

I tried writing the program but cannot work. I am stuck with this for several days already.

This is an incomplete program. Can you pls help me out with this?

Thank you very much.

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;
int main() {
      std::ifstream ifs("c:\\FT_student.pdf", std::ios::binary);
      char buff[1024];
      ifs.seekg (0, std::ios::beg);
      int count = 0;
      while(!ifs.eof()){
                        ifs.getline(buff,1024);
                        std::cout << count++ << std::endl;
                        }

    cin.ignore();
    cin.get();
    return 0;
}

Dani AI

Generated

A reliable approach is to treat the file as raw bytes and scan it with a sliding window. Text functions (line reads) are unsafe for binary data: they stop at newlines and NULs and can produce wrong results. The target sequence 0x000001 is the three bytes 0x00, 0x00, 0x01; the scanner must find that exact byte sequence anywhere in the file, including when the pattern crosses a buffer boundary.

Read fixed-size blocks (for example 4KB or 64KB), and on each read scan for the 3-byte pattern. Keep the last (pattern_length - 1) bytes from the previous block and prepend them to the current block before scanning so you never miss a match that straddles a boundary. Use unsigned byte comparisons (cast to unsigned char or uint8_t) and check the actual number of bytes read with gcount() rather than relying on eof() or line-oriented helpers. For raw reads use basic_istream::read (see basic_istream::read). For larger or more complex patterns C++17 provides std::search / pattern searchers (see std::search), but for a three-byte signature a simple linear scan is fine and fast.

Example scanning pattern (illustrative):

// open file, read blocks into a vector<uint8_t>, keep a 2-byte tail,
// prepend tail to the current buffer and scan for 0x00,0x00,0x01.
// update tail to be the last two bytes of the current buffer.

Comments on earlier replies: used a line-oriented read which explains the trouble. was right to think in terms of fixed-size reads if the file is structured; ’s point about file size is useful for progress/math but not needed for the basic scan; ’s pattern-typing idea is neat and expressive, but remember to handle chunk boundaries and use gcount()—otherwise a match spanning blocks can be missed.

Recommended Answers

All 4 Replies

What will the contents of the file be like? You can try something like this:

i assume they are on new lines. So, is it 4bytes, separated by a newline character?

So, you can read 4 bytes. Read it into a buffer. say

unsigned char buff[4];

Now, check for your required condition using the >> operator.
Scan through all the bits.

OR

Another option would be to use bitset.

A binary file may contain multiple eof characters. So take the size of the file and loop to the total size / till you find the required binary number.

First create a user defined type to represent 23 zero bits and 1 one bit.

#include <cstdint> // C++1X (or <stdint.h> for C99)
struct pattern
{
    enum { NBYTES = 3 } ;
    std::uint8_t bytes[NBYTES] ; // 3 x 8 bits == 24 bits
};

Then provide mechanisms to read such a variable from an input stream and to compare two such variables for equality.

std::istream& operator>> ( std::istream& stm, pattern& p )
{ return stm.read( reinterpret_cast<char*>( p.bytes ), pattern::NBYTES ) ; }

bool operator== ( const pattern& first, const pattern& second )
{ return std::equal( first.bytes, first.bytes + pattern::NBYTES, second.bytes ) ; }

Finally, write the function to return a "yes" or a "no"

const char* find_pattern( const char* path_to_file )
{
    std::ifstream file( path_to_file, std::ios::binary ) ;
    std::istream_iterator<pattern> begin(file), end ;
    static const pattern search_pattern = { { 0, 0, 1 } } ; // 23 zero bits and 1 one bit
    return std::find( begin, end, search_pattern ) == end ? "no" : "yes" ;
}
commented: Helpful advise. Thanks a lot! +2

Hi all,
thanks for your help. i will try to do it again with all of your guidance. Thanks again.

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.