I want to write k elements starting from k*i to a file. so in a for loop I write as below:
testOut.write((char*) h_test[k*i] ,k*sizeof(float));
I get this error "invalid cast from type ‘float’ to type ‘char*’ "
How should I correct this?
If you want to write them in binary, you can do as follows:
testOut.write( reinterpret_cast<const char*>(h_test + k*i), k*sizeof(float));
h_test + k*i is the address of a float pointer, that you reinterpret to a const char* so that write can take it. Note that adding to a pointer increments the pointer taking into account the pointed element size, and you convert the pointer after.
Finally, write accepts a const char*, not char*.