Hi i have my code that finds every instance of my search criteria in a text file and tells me the position in the text file it is located at. The next step i want to do is stream the bytes that follow each instance of my search criteria into a new file until it reaches another search criteria.

My example is that i have lots of jpeg's inside a text file full of data. I presently search for the start of every picture. I then want to stream the data into a new file until i reach the end of every picture and recreate all the pictures that may be embedded in the text file. I hope im explaining myself well enough.

Can anyone show me how to set up a file stream that will start at every point of my search criteria and stop and the end of the data i need which is my 'match_criteria1'

thanks

static char match_criteria1[] = { 0xFF, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  };
static char match_criteria[] = { 0xFF, 0xD8, 0xFF, 0xC0 };
 

 size_t buf_len = (sizeof( match_criteria )/sizeof( char ));
 char* p_read_buffer = new char[buf_len];

size_t buf_len1 = (sizeof( match_criteria1 )/sizeof( char ));
 char* p_read_buffer1 = new char[buf_len1];
 


	int pos = 0;
	bool matched = false;
	

	


    cout << "Enter the name of the file you wish to open: ";
    string filename;
    getline( cin, filename );




    ifstream infile( filename.c_str(), ios::binary );
   

int position = 0;
		











    while( infile.is_open() && infile.good() && !infile.eof() && !infile.fail() && !infile.bad() )
    
{

	 infile.seekg(position, ios::beg);
         infile.read( p_read_buffer, buf_len );
	
	
	 if( infile.eof() || infile.fail() || infile.bad() )
	{
	    infile.close();
	    
	}
	
else if( memcmp( p_read_buffer, match_criteria, buf_len ) == 0 )
	{

	
	    matched = true;
	    pos = infile.tellg();
	    pos -= buf_len;
	  
	
	  
target_file.seekg (pos,std::ios::beg) ;

	


cout << "File: " << filename << " match search criteria at position: " << pos << endl;


}




position++;


    }

> i have lots of jpeg's inside a text file full of data. I presently search for the start of every picture.
> I then want to stream the data into a new file until i reach the end of every picture ...
> the end of the data i need which is my 'match_criteria1'
how are you sure that the byte sequence specified by 'match_criteria1' is not present in the jpeg image? can't you just read the jpeg header instead?

to stream all the bytes between a start byte sequence and and end byte sequence, the simplest way would be to use a sliding window.

#include <deque>
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std ;

int main()
{
  typedef unsigned char byte ;
  static byte match_1[] = { 0xFF, 0xD9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  };
  static byte match_0[] = { 0xFF, 0xD8, 0xFF, 0xC0 };
  enum { SIZE_0 = sizeof(match_0), SIZE_1 = sizeof(match_1) } ;
  std::ifstream fin( "infilename", std::ios::binary ) ;
  fin >> std::noskipws ;
  std::ofstream fout( "outfilename", std::ios::binary ) ;

  byte ch ;
  std::deque<byte> swindow ;

  while( ( swindow.size() < SIZE_0 ) )
  {
    if( !( fin >> ch ) ) return 1 ;
    swindow.push_back( ch ) ;
  }

  // note: could use std::search instead of std::memcmp
  while( std::memcmp( &swindow.front(), match_0, SIZE_0 ) != 0 )
  {
    if( !( fin >> ch ) ) return 1 ;
    swindow.push_back( ch ) ;
    swindow.pop_front() ;
  }

  swindow.clear() ;
  while( ( swindow.size() < SIZE_1 ) )
  {
    if( !( fin >> ch ) ) return 2 ;
    swindow.push_back( ch ) ;
  }

  while( std::memcmp( &swindow.front(), match_1, SIZE_1 ) != 0 )
  {
    if( !( fin >> ch ) ) return 2 ;
    swindow.push_back( ch ) ;
    fout << swindow.front() ;
    swindow.pop_front() ;
  }
}

i know that match_criteria 1 wont appear because all of the images in my text file are separated by 11 bytes of 00x0. Thank you for your help, however ive never herd of a sliding window before and i cant understand how its working? Ive given it ago and it picks up the first image in my file. To get it to loop through my file and find every picture would i have to put it in a loop until EOF?

thank you

> ive never herd of a sliding window before
the sliding window algorithm is perhaps the most important algorithm used in the implementation of the TCP protocol. it is inefficient for the sender to wait after each packet for its ACK before sending the next. a sliding window is used to keep a record of the packet sequences sent and the respective ACKs received.

as an example, if we take the trivial case of a fixed window size M: a sender may sent M packets ( packet numbers N ... N+M-1 ) before receiving any ACK. If ACK is received for packet N, the window slides to N+1 ... N+M, and the sender can transmit packet N+M. sliding signifies a FIFO operation, trimming the range at one end (closing the window), and extending it at the other (opening the window).

> and i cant understand how its working?
consider simulating the program flow using pencil and paper.

> To get it to loop through my file and find every picture would i have to put it in a loop until EOF?
right.

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.