lundi 1 juillet 2019

Read Every 2 Bytes from Data file and compare in a vector

I am new to C++. I have an assignment where I have to read a Binary data file and in the data file I have to read every 2 Bites and check for a header value. I have written following code but I am not getting correct data. can some one please guide me how to approach for this problem.

std::vector<char> checkValues = { (char)0xCA5E , (char)0xBEAD };
std::ifstream input("abc.dat", std::ios::binary | std::ios::in);
if (input)
{
    input.seekg(0, ios::end);
    size_t fileSize = input.tellg();
    input.seekg(0, ios::beg);
    /*How to run loop here for every two byte*/
    {
        char * buffer = new char[2];
        input.read(buffer, sizeof(buffer) / sizeof(*buffer));
        std::vector<char>::iterator it = std::find(checkValues.begin(), checkValues.end(), buffer);
        if (it != checkValues.end())
            std::cout << "Element Found" << std::endl;
        else
            std::cout << "Element Not Found" << std::endl;
        delete[] buffer;
    }
}

I have doubts about my approach, I am still learning. Please if you can guide me towards a better solution that would be helpful. Thank you.

Aucun commentaire:

Enregistrer un commentaire