-2

My current code is able to read it in but when I try to put it into the frequency table some of the values are incorrect.

unsigned int mFrequencyTable[256] = { 1 };
ifstream bin(mFileName, ios::binary);
        int index = 0;
        unsigned char val;
        unsigned char val2;
if (bin.is_open() == true) {
            while (!bin.eof()) {
                bin.read((char*)&val, sizeof(char));
                while (!bin.eof()) {
                    bin.read((char*)&val2, sizeof(char));
                    mFrequencyTable[val2];
                    index++;
                }
                break;
            }
        }
C p
  • 1
  • https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – πάντα ῥεῖ May 28 '22 at 18:07
  • 1
    Why do you have two loops? You throw away the first value you read. `mFrequencyTable[val2]` doesn't do anything useful. Did you mean to increment that value instead? `while (bin.read((char*)&val, sizeof(char))) { ++mFrequencyTable[val]; }` should be all you need. You probably want to use `0` instead of `1` when you initialize `mFrequencyTable` or the first index will be off by one. – Retired Ninja May 28 '22 at 18:07
  • the mFrequency table was zeroed out previously – C p May 28 '22 at 21:21
  • Based on the code you've shown the the table is initialized with the first element set to 1 and the rest 0. Perhaps there's other code that does something to prepare the table before you try to fill it in but I can only comment on the code I can see. – Retired Ninja May 29 '22 at 04:55

0 Answers0