conversion....and reading raw data from file?

Started by
7 comments, last by Mike-21 21 years, 6 months ago
How can i convert from a void* to a float or double? from void* to int it work perfecttly (i use reinterpret_cast to do the job) but it won''t work with a float... hwo can i convert it? and can anyone tell me how can a read in a raw variable.. (i mean kind of byte by byte) i''ve try something like this : unsigned char* data= new unsigned char[m_dataSize * sizeof(unsigned char)]; and when i add the data to it it won''t contain nothing (by the way i reading file with the fstream in binary mode)
Advertisement
quote:Original post by Mike-21
How can i convert from a void* to a float or double?

What for? I can't think of any circumstances you'd ever want to do this.
quote:
and can anyone tell me how can a read in a raw variable..
(i mean kind of byte by byte)

Depends what you mean by "raw variable". If you want to read an int, for example, you can use:

int i;cin >> i;  


If that's not what you mean, its probably best you post the code you've already written so that we can see why it doesn't work.

[edited by - SabreMan on October 18, 2002 11:48:40 AM]
well its simple, it''s because i''m creating an archive system
so i must read the data from the file (i want to include in the new archive)
and write it back in binary but before writing back, i must hold it in a variable!
and why i want to do void* to float is because when i read back from my archive file, i must convert back the data i just came to read since it was in a void* i put the data read
so that it(understand?)


and by the way, cin, cout are for screen display not file
quote:Original post by Mike-21
well its simple, it''s because i''m creating an archive system
so i must read the data from the file (i want to include in the new archive)

So, what exactly is your question? You said you wrote something that doesn''t work. Where''s the code?
quote:
and why i want to do void* to float is because when i read back from my archive file, i must convert back the data i just came to read since it was in a void* i put the data read
so that it(understand?)

What''s it doing in a void* in the first place? In fact, let me rephrase that. Don''t use fread() with C++!
quote:
and by the way, cin, cout are for screen display not file

Yes, but operator>> is overloaded for filestreams.
use fstream::read() if you must read binary data.

although, I suppose you could write a custom traits class for your file format.
daerid@gmail.com
Well deadric, to read, write data in binary mode i know how and of course i use read write function from the fstream and it write perfectly, but it's when i read it back, since i don't know the type the data was written in when the writting occured, (and this is normal in my case)
then i must read this data into a kind of universal variable of no real type (but i can't use template for this job)
so i tough of his C equivalent void* but my problem is when i read from my archive file and that the data is a float it read it in the void* (adresse change in the variable view)but reinterpret_cast don't allow me to convert from void* to a float type.

NOTE: an archive file containt X number of files and each files contain all kind of data, either sound, bmp, or stats files that can contain a lot of different information that was contain in different type of variable)

[edited by - mike-21 on October 18, 2002 2:26:16 PM]
Why don''t you know the type the data was written in? Aren''t you using some kind of standardized file format (even if it''s one you came up with) ?
daerid@gmail.com
I don't know the data type within my archive system
all it do is write the data in question and permit me to reload it in memory, and pass it to me, so it's up to me to transform back this data to the format it should be.
and that why i need a sort of way to conserve this data in my archive system, i've tried with an array of unsigned char* large enough to contain the file data, but it's not working..
i'used this since i taught that an unsigned char is 1 byte long and in binary format, we read by byte.. but it don't seem to work or i don't do it corectly. I do it like this:

unsiged char* data= new unsigned char[dataSize * sizeof(unsigned char)]

stream.read(reinterpret_cast(&data), dataSize * sizeof(unsigned char));

my stream variable is okay, i've test if it failed at creation or opening and verything is fine

so this should create an array of X bytes, the good size to contain all my file data..and this is not working.. so maybe i don't understand well how to read byte by byte in binary mode...?


[edited by - mike-21 on October 18, 2002 3:24:40 PM]
Your reinterpret cast from void* to int is probably returning the address of the pointer. Both are probably 32 bits.

Trying to reinterpret a void* to a float/double doesn''t work because a pointer isn''t the same size as a float/double. You want to reinterpret cast the dereferenced value of your pointer...not the pointer itself.

This topic is closed to new replies.

Advertisement