Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer structures in Borland C++

Started by bosjoh Dec 19, 1999 at 5:58 PM 9 replies 1.3k views
Original Post
bosjoh
bosjoh
When I try for example to read a structure from a file (method used by many) with fstream the compiler gives errors. I've typed this:

fstream file;
file.open("test.tst",0x81);
file.read(&struct,sizeof(struct));
file.close();

Many other C++ compilers doesn't seem to have problems with this, only borland's. Any way to solve this?

Gromit
Gromit
What are the errors that you are getting?
William Reiach - Human Extrodinaire

Marlene and Me

skitzo_smurf
skitzo_smurf
You're not getting type mismatch errors are you? I dont know if this'll help, but heres some documentation for that function:

Syntax
istream& read(char*, int);
istream& read(signed char*, int);
istream& read(unsigned char*, int);

Description

The read member function extracts a given number of characters into an array. Use gcount() for the number of characters actually extracted if an error occurred.

"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
bosjoh
bosjoh
It gives the following error:
Can not find a match for
fstream::read(STRUCT*,unsigned int)

And there is just no way to convert the STRUCT* to a void* or char*.

Starfall
Starfall
What happens if you use (char*)(&struct)? I don't see any reason why this wouldn't work, unless I'm missing something here...

Good luck

Starfall

Xai
Xai
Starfall is 100% correct. What do you mean there is NO WAY to convert struct* to void* or char*...ALL pointers can be cast to ALL other pointers....it's just part of C....assuming your not in a system with near and far pointers

you just say this:
if( file.read((char*)&myStruct,sizeof(STRUCT)) != sizeof(STRUCT) )
// then you had an error

good luck

bosjoh
bosjoh
Thanx for all your replies.
And the solution was JUST the thing that I haven't thought about. You see: the solution is in the place where you least expected.
I had tried things like:

char *ptr;
MYSTRUCT mstruct;

ptr=&mstruct

That wouldn't work. But type casting does work.

joeG
joeG
You may also want to investigate
code:
reinterpret_cast (var_struct)

, as I've heard it provides a safer way to cast un-related types (in your case your struct type).

[This message has been edited by joeG (edited December 19, 1999).]

joeG
Starfall
Starfall
char *ptr;
MYSTRUCT mstruct;

ptr=&mstruct

This should also work, if you want to do it this way - just use:

ptr = (char*)&mstruct

Regards

Starfall

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.