• ### Popular Now

• 12
• 14
• 13
• 10
• 11

#### Archived

This topic is now archived and is closed to further replies.

# void *data used in fread, can it be cast to a long...

This topic is 5315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am coding a LWO2 reader for lightwave but am confused by the lp_fileSize output. The variable lp_fileSize is a long .. so i read in the long *data. bytesRead = fread(lp_fileSize,1,4,*fp) This is assuming i am at the right position in the file to get a 32bit value. but cout<

##### Share on other sites
I can''t understand your question really, but generally C/C++ allows you to do all kinds of sick type conversions. void* is used to denote that a conversion has to be made since this type cannot be used for manipulation directly. Ergo:

maybe this explains it to you (code not checked and written at 5 AM) *g*
const int BufferSize = 40000;char* Data = new char[BufferSize];int bytesRead = fread((void*)Data, 1, 4, fp);//if Data were void*, we''d have to cast it now//to access it (since the size of void is undefined)//if you want to use the first four bytes od Data as a //long data type, you''ll need to do the necessary//conversion by some non-standard means,//such as using a macro. For an example see the//MySQL screensaver tutorial (main page on the left) and //Ctrl+F for a macro called MAKEDEC

##### Share on other sites
Its not that i cant read data of type char, but that when i read in a long variable it is backwards and i reverse it with a method call.. but when I output the long variable it is most definately incorrect...???

Should i be using a 32bit unsigned integer or 2 16bit integers to get the correct numeric data ?

##### Share on other sites
quote:
Original post by OverGround
long *data.

I don''t think you''re using the fread() function in the proper manner.
The function is constructed like this:

int fread (void * buffer, size_t size, size_t count, FILE * stream);

The fread() function returns an int value which is the number of items its read.

So.. bytesRead will be the number of items read (in this case it will be 4)

(void * buffer, size_t size, size_t count, FILE * stream)

The buffer in this case, where you''ve got lp_fileSize needs to be a char * not a long .

I pressume you also want the 32bit value aswell?
Are you talking the binary 1''s and 0''s or just the 4 bytes of data?

You''ll need to switch the 1 and 4 around to 4 and 1.

unsigned char *buffer;NumberOfItemsRead = fread(buffer,4,1,*fp);cout << buffer << endl;

This will insert 4 bytes into the buffer variable.

The cout method should display the 4 bytes as characters.

Now, also the NumberOfItemsRead is = 1. (Because we switched them)

Capturing the return value isn''t necessary to run the function either.

I''m not sure what you were intending with the lp_fileSize variable?
Did you want to jump to a certain position in the file?

Also if you want to convert the buffer into a long value?
I don''t know other ways to do this but one way would be to put the buffer into a String and then convert that to a Double .

Hope this helps.

- ]Reaper[

##### Share on other sites
quote:
Original post by OverGround
Its not that i cant read data of type char, but that when i read in a long variable it is backwards and i reverse it with a method call.. but when I output the long variable it is most definately incorrect...???

Should i be using a 32bit unsigned integer or 2 16bit integers to get the correct numeric data ?

I think you should reread the comment in the code block in my previous post...

##### Share on other sites
If i''m guessing correctly. you just want to read a long variable from a file?

if so, try this. it works for me.

long FileSize;

##### Share on other sites
quote:
Original post by Crispy
I think you should reread the comment in the code block in my previous post...

I don''t quite get what your comments mean - non standard conversion macro? such as? A macro can be used to perform conversions, but it has more to do with the specific macro, that is, it''s not a macro thing. A macro merely substitutes tokens. It''s still code underneath.

"when i read in a long variable it is backwards" - that sounds little endian

in regards to

int fread (void * buffer, size_t size, size_t count, FILE * stream);

the "void * buffer" means that you can pass a buffer array of any type to the function without having to cast. Consider:

const int length = 1000;
long buffer[length];

elements_read can be zero, in which case nothing was read from the file, or it can be less than or equal to length. elements_read tells you how many elements of the buffer have been filled.

by extension

const int length = 1000;
myfunkystructtype buffer[length];

##### Share on other sites
quote:
I don''t quite get what your comments mean - non standard conversion macro? such as? A macro can be used to perform conversions, but it has more to do with the specific macro, that is, it''s not a macro thing. A macro merely substitutes tokens. It''s still code underneath.

My specific wording was: "you''ll need to do the necessary conversion by some non-standard means, such as using a macro", which is not the same as "non standard conversion macro". I tried to specific about that and I think I was. Macros may be a part of the C/C++ language, but (I''m shooting into the dark here) AFAIK they''re not standardized. That means, coming up with a macro to do the conversion is non-standard. The macro you use itself is not non-standard.

Anyway, since this thing is already generating that much discussion, I''ll just post the macro myself. Seems like checking it out is a wee bit too difficult for people.

#include <windows>#define MAKEDEC(a) MAKELONG(MAKEWORD(a[0], a[1]), MAKEWORD(a[2], a[3]));

Note that this macro doesn''t test if a is NULL in which case your program will crash.

Now just read like everyone here have shown you, an array of n bytes and pass it to MAKEDEC - the first four bytes are reversed to form your number of type 32-bit integer.

quote:

"when i read in a long variable it is backwards" - that sounds little endian

I wouldn''ve said that myself, but I always get the two mixed up...

##### Share on other sites
Thanks for the help my endian problem was because of the IFF for lightwave and reading the long could be a 2 words which would work for a 16 bit console was helpful.

If the fread(&long,4,1,*fp) works as you say then im not doing ne thing wrong except using a win console cout<< statement to print my long variable.... is this why i get garbage in the output?????

Thanks all
PS.. Im working the lightwave loader and if you would like to see some lwo models my aim is -->>> PitJedi221 <<<--Look me up..

Im still here?