| | |
memcmp help needed
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 78
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
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++; }
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
> 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.
c++ Syntax (Toggle Plain Text)
#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() ; } }
•
•
Join Date: Nov 2007
Posts: 78
Reputation:
Solved Threads: 0
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
thank you
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
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.
Last edited by vijayan121; Mar 28th, 2008 at 5:54 pm.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Counter help
- Next Thread: Undeclared Identifier
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






