Search code examples
c++integerboolean

How can I fix an int-to-bool warning in C++?


I get a warning in MSVC++ when I try to read an integer from a file and make a bool variable equal it.

accessLV[i] = FileRead(file1, i + 1);

(accessLV is an array of bools, FileRead is a function I made to decrease the syntax involved in reading from a file, i is because the statement is within a for loop)

I've tried using a static_cast:

accessLV[i] = static_cast<bool>(FileRead(file1, i + 1));

But I still get the warning. I've tried doing this (I'm not sure the exact term):

accessLV[i] = (bool)FileRead(file1, i + 1));

And the warning is still there. Is there anyway to get rid of the warning without making accessLV an array of ints?

NB: this is the syntax of FileRead, if it helps:

int FileRead(std::fstream& file, int pos)
{
    int data;
    file.seekg(file.beg + pos * sizeof(int));
    file.read(reinterpret_cast<char*>(&data), sizeof(data));
    return data;
}

Solution

  • How about

    accessLV[i] = FileRead(file1, i + 1) != 0;