Pointer structures in Borland C++

Started by
9 comments, last by bosjoh 20 years, 3 months ago
What are the errors that you are getting?
William Reiach - Human Extrodinaire

Marlene and Me


Advertisement
Well, gee, it's kinda hard to tell since you didn't tell what the error messages are, but I suspect that the 0x81 has something to do with them.
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
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*.

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

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

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.

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
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?

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

This topic is closed to new replies.

Advertisement