0

Here is my code:

    ifstream ifile;
    list<unsigned char> fbinary;

    ifile.open(filename, ios::binary);
    if (ifile.fail() || !ifile.is_open())
        return false;

    ifile.seekg(0, ios::end);
    int sz = ifile.tellg();
    if (sz <= 0)
        return false;

    try
    {
        //Get Binary And Encrypt with XOR
        ifile.seekg(0, ios::beg);
        unsigned char temp = 0;
        while (ifile)
        {
            ifile.read((char*)temp, sizeof(unsigned char));
            temp ^= Encrypt_Key;
            fbinary.push_back(temp);
        }

        ifile.close();
    }
    ...

file.open and getting file size don't fail. I don't know why ifile.read() fires debug assertion.

Error Message:

Expression : buffer != nullptr

Evg
  • 25,259
  • 5
  • 41
  • 83
64Bit O2
  • 29
  • 1
  • 5
  • 1
    I see neither debug assertion, nor a variable `buffer` in this code. – Evg Nov 03 '19 at 16:14
  • Please provide the full error message. There are going to be more lines before or after the one you included in the question. Also a [repro] is required here for debugging help. – walnut Nov 03 '19 at 16:18
  • 2
    The linked image gives a 404 and you are supposed to edit the error message as plain text into the question. See [ask]. – walnut Nov 03 '19 at 16:21
  • @64BitO2 Any relevant code (and error message) should be *in the question*, as *text*. Not behind external links. – Jesper Juhl Nov 03 '19 at 16:23
  • General advice: If you write an explicit cast like in `(char*)temp`, you better make really really sure that you know what it is doing. I think you didn't do that here. Also prefer `static_cast<...>(...)` instead of C-style casts, as they are a bit safer. – walnut Nov 03 '19 at 16:24
  • 1
    `(char*)temp` should be `&temp`. – Evg Nov 03 '19 at 16:26
  • @Evg &temp is unsgined char* and (char*)temp is char*. not same – 64Bit O2 Nov 03 '19 at 16:30
  • @64BitO2 Then why are you not declaring `temp` as `char`? – walnut Nov 03 '19 at 16:31
  • @uneven_mark, you're right. :) Moreover, it will always fail, because `char` and `unsigned char` are unconditionally different types. – Evg Nov 03 '19 at 16:33
  • @Evg Oh right, it doesn't actually matter, it is always wrong. – walnut Nov 03 '19 at 16:35
  • @uneven_mark i changed unsgined char to char, and it works. but i don't understand what is the matter. – 64Bit O2 Nov 03 '19 at 16:41

2 Answers2

4

It is not clear what buffer is (it's likely to be a variable inside the standard library implementation), but the code in the question has at least one blatant error:

unsigned char temp = 0;
... 
ifile.read((char*)temp, sizeof(unsigned char));

Here you first initialize temp with zero, then convert it into a pointer, which is actually a null pointer. Calling .read() with a null pointer is UB - there is no valid memory buffer at the location (char*)temp (= nullptr).

What you want, is probably this:

char temp;
... 
ifile.read(&temp, sizeof(char));

Here you pass the valid address of temp into .read().

Also note that getting file size from tellg() can give an incorrect result. See this question.

Evg
  • 25,259
  • 5
  • 41
  • 83
-4

Try calling ifile.clear() before you seek the beginning, inside the try block:

try { // call clear ifile.clear(); //Get Binary And Encrypt with XOR ifile.seekg(0, ios::beg); unsigned char temp = 0; while (ifile) {

emirc
  • 1,948
  • 1
  • 23
  • 38