I would certainly go with "hide the actual type", in other words, use a map
, vector
or some similar type to store the FILE *
, and pass the index
or whatever is needed to retrieve the FILE *
for the calls that need it. That way, it won't matter HOW you open the file (you may later want to use fstream
and C++, for example, which will be much harder to store as a copy of some sort).
How to convert FILE* to numeric handle in 64 bit system?
-
28-09-2022 - |
Pergunta
I use embedded JavaScript engine, and I used to pass the result of fopen()
to JS code as a Number.
int numeric_handle = (int)fopen(*P1, "rb");
return Number::New(numeric_handle); // Return to JavaScript
But now I need to recompile in 64 bit system, and I get
fatal error: cast from pointer to smaller type 'int' loses information
Now I am sort of stuck, how do I keep the design of my system but change as little code as possible? I was thinking of having a hash-map, an array that holds FILE* and return an ID in that array. But that will mean extra memory management, extra lookup.
Solução 3
Outras dicas
long should be enough, but you can use a byte buffer too
FILE *f;
char buffer[sizeof(f)];
memcpy(buffer, &f, sizeof(f));
Why do you need such a hack?
Use a pointer sized integer like intptr_t from cstdint.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow