My compiler complains about possible data loss when I assign as follows:
SIZE_T* size = new SIZE_T;
std::ifstream::pos_type fileSize = file.tellg();//This works fine
char* fileContents = (char*)std::malloc(fileSize * sizeof(char));//Warning 1 warning C4244: 'argument' : conversion from 'std::streamoff' to 'size_t', possible loss of data
*size = fileSize;//Also complains: "Warning 2 warning C4244: '=' : conversion from 'std::streamoff' to 'SIZE_T', possible loss of data."
So is there a way to safely convert variable from type std::ifstream::pos_type
to size_t
. I could just be rude and tell the compiler to keep quiet about this by using explicit c style casting, but I wonder if it is safe?
My code compiles and runs correctly, but as value in fileSize
grows when bigger files are read this could break right? Is this the unsafe way to get the file size and is there any better approach to this?
Aucun commentaire:
Enregistrer un commentaire