0

I am trying to read a binary file's data sadly opening in C++ is a lot different than in python for these things as they have byte mode. It seems C++ does not have that.

for (auto p = directory_iterator(path); p != directory_iterator(); p++) {
    if (!is_directory(p->path()))
        byte tmpdata;
        std::ifstream tmpreader;
        tmpreader.open(desfile, std::ios_base::binary);
        int currentByte = tmpreader.get();
        while (currentByte >= 0)
        {
            //std::cout << "Does this get Called?" << std::endl;
            int currentByte = tmpreader.get();
            tmpdata = currentByte;
        }
        tmpreader.close()
    }
else
{
    continue;
}

I want basically a clone of Python's methods of opening a file in 'rb' mode. To have to actual byte data of all of the contents (which is not readable as it has nonprintable chars even for C++. Most of which probably cant be converted to signed chars just because it contains zlib compressed data that I need to feed in my DLL to decompress it all.

I do know that in Python I can do something like this:

file_object = open('[file here]', 'rb')

turns out that replacing the C++ Code above with this helps. However fopen is depreciated but I dont care.

What the Code above did not do was work because I was not reading from the buffer data. I did realize later that fopen, fseek, fread, and fclose was the functions I needed for read bytes mode ('rb').

for (auto p = directory_iterator(path); p != directory_iterator(); p++) {
    if (!is_directory(p->path()))
    {
        std::string desfile = p->path().filename().string();
        byte tmpdata;
        unsigned char* data2;
        FILE *fp = fopen("data.d", "rb");
        fseek(fp, 0, SEEK_END); // GO TO END OF FILE
        size_t size = ftell(fp);
        fseek(fp, 0, SEEK_SET); // GO BACK TO START
        data2 = new unsigned char[size];
        tmpdata = fread(data2, 1, size, fp);
        fclose(fp);
    }
else
{
    continue;
}
3
  • 1
    What is the problem with the code you posted? Commented Aug 24, 2016 at 11:56
  • It does not do exactly what I want, eg gets wrong stuff and it seems it gets litterally no data from the files at all. Commented Aug 24, 2016 at 11:59
  • 2
    It is difficult to offer solutions when the problem statement is simply, "it doesn't work". Please edit your question to give a more complete description of what you expected to happen and how that differs from the actual results. See How to Ask for hints on what makes a good explanation. Commented Aug 24, 2016 at 18:04

1 Answer 1

5
int currentByte = tmpreader.get();
while (currentByte >= 0)
{
    //std::cout << "Does this get Called?" << std::endl;
    int currentByte = tmpreader.get();
    //^ here!

You are declaring a second variable hiding the outer one. However, this inner one is only valid within the while loop's body, so the while condition checks the outer variable which is not modified any more. Rather do it this way:

int currentByte;
while ((currentByte = tmpreader.get()) >= 0)
{
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.