hi all ,
i have a program that read its input from a file in binary mode!
now i nead to store this data and to compare it with another data.
which datataype can my input take.

also, the comapred stream is in this form 0004a4900
i made this stream as a cons int and then with 0x it is already stored as an hex. stream
now when i read i want to search this strream in my input file

Dani AI

Generated

Brief summary and practical approach for (building on 's comment about signedness):

Treat the file as a sequence of bytes, not text. For binary comparison use a byte container such as std::vector<unsigned char> (or std::vector<std::byte> in C++17+) so values range 0..255 and comparisons are unambiguous. Convert your hex pattern (the "0004a4900" string) into a sequence of bytes, then search that byte sequence in the file bytes.

Example workflow (read file, parse hex, search):

#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>

// read entire file into vector<unsigned char>
std::ifstream in("input.bin", std::ios::binary);
in.seekg(0, std::ios::end);
std::vector<unsigned char> data(in.tellg());
in.seekg(0);
in.read(reinterpret_cast<char*>(data.data()), data.size());

// convert hex string to bytes (pad left if odd length)
std::string hex = "0004a4900";
if (hex.size() % 2) hex = "0" + hex;
std::vector<unsigned char> pattern;
for (size_t i = 0; i < hex.size(); i += 2) {
  unsigned int byte = 0;
  std::istringstream(hex.substr(i,2)) >> std::hex >> byte;
  pattern.push_back(static_cast<unsigned char>(byte));
}

// search
auto it = std::search(data.begin(), data.end(), pattern.begin(), pattern.end());
bool found = (it != data.end());

Notes and cautions:

  • If you originally created the pattern as an integer literal (e.g., 0x...) be careful: comparing that integer to raw file bytes requires you to pick an endianness and split the integer into bytes accordingly. Direct byte-sequence comparison avoids that pitfall.
  • For very large files read in chunks and use a sliding overlap of pattern.size()-1 bytes so matches crossing chunk boundaries are not missed.
  • On modern C++ you can use std::byte for clearer intent; for searching std::search (or C++17 searchers) works on any byte iterator (std::search, std::byte).

Recommended Answers

All 5 Replies

>which datataype can my input take.
The same data type you used to write it if you want a meaningful comparison. If you're just checking for equality, you can use unsigned char and compare every byte individually.

>also, the comapred stream is in this form 0004a4900
>i made this stream as a cons int and then with 0x it is already stored as an hex. stream
>now when i read i want to search this strream in my input file
There's no question here.

unsigned char is for byte or what!!
i mean what is the difference between char and unsigned char?

>i mean what is the difference between char and unsigned char?
char isn't portable because it could be either signed or unsigned.

and what does unsigned char mean?

is each byte 'll read as an unsigned char?

Yes.

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.