type conversion

J

JasBascom

if i have an ifstream object

ifstream validdata;
const int list = 5;
Allrecords books;
Allrecords *rec = books
  • ;

    if i had the following code

    validdata.seekg(0, ios::end);
    filesize = validdata.tellg();
    validdata.seekg(offset, ios::beg);

    reccount = filesize/sizeof(Allrecords);
    rec = new(Allrecords[reccount]);

    validdata.read((char*) rec, filesize);


    the last line is causing a debug assertion error. without this line the code
    executes ok. What I would like to know is there another way of reading a union,
    using the read function. Or a type conversion for (char*)
 
V

Victor Bazarov

JasBascom said:
if i have an ifstream object

ifstream validdata;
const int list = 5;
Allrecords books;
Allrecords *rec = books
  • ;

    if i had the following code

    validdata.seekg(0, ios::end);
    filesize = validdata.tellg();
    validdata.seekg(offset, ios::beg);

    reccount = filesize/sizeof(Allrecords);
    rec = new(Allrecords[reccount]);


  • Did you mean to say

    rec = new Allrecords[reccount];

    ?
    validdata.read((char*) rec, filesize);


    the last line is causing a debug assertion error. without this line the code
    executes ok. What I would like to know is there another way of reading a union,
    using the read function. Or a type conversion for (char*)

    What's "Allrecords"? How come its operator[] returns a pointer
    to an object of the same type? This is highly unusual. And
    just like many times before, with such fragment of a program it
    is generally not possible to recommend anything. Try to follow
    the guidelines given in FAQ 5.8.

    Victor
 
J

Jonathan Turkanis

JasBascom said:
if i have an ifstream object

ifstream validdata;
const int list = 5;
Allrecords books;
Allrecords *rec = books
  • ;

    if i had the following code

    validdata.seekg(0, ios::end);
    filesize = validdata.tellg();
    validdata.seekg(offset, ios::beg);

    reccount = filesize/sizeof(Allrecords);
    rec = new(Allrecords[reccount]);

    validdata.read((char*) rec, filesize);


    the last line is causing a debug assertion error. without this line the code
    executes ok. What I would like to know is there another way of reading a union,
    using the read function. Or a type conversion for (char*)


  • It looks like you're code is probably doing something nasty. This
    "Allrecords *rec" has figured in several recent messages, all rather
    crytpic. It would help to see the actual definition of Allrecords
    (make it as short as possible for demonstration purposes.)

    Jonathan
 
J

John Harrison

JasBascom said:
if i have an ifstream object

ifstream validdata;
const int list = 5;
Allrecords books;
Allrecords *rec = books
  • ;


  • Huh? What does the above line mean?
    if i had the following code

    validdata.seekg(0, ios::end);
    filesize = validdata.tellg();
    validdata.seekg(offset, ios::beg);

    reccount = filesize/sizeof(Allrecords);
    rec = new(Allrecords[reccount]);

    validdata.read((char*) rec, filesize);


    the last line is causing a debug assertion error. without this line the code
    executes ok. What I would like to know is there another way of reading a union,
    using the read function. Or a type conversion for (char*)

    No, you are doing it the right way, the problem must be somewhere else in
    your code.

    I have explained this twice before, you are trying to write too much
    untested code, so when a problem occurs you have no idea where the problem
    actually is. If you just wrote a few lines at a time and spent a little time
    to make absolutely sure that they were working before you wrote any more
    code, then you wouldn't be running into these problems.

    Since I doubt you are going to change your approach at this late stage (but
    hopefully you will for the next program you write) why not post the entire
    program, hopefully someone will take a look and at least get you past the
    debug assertion error.

    john
 
J

Jonathan Turkanis

Jonathan Turkanis said:
It looks like you're code is probably doing something nasty. This

Ughh! I mean 'your'

It's hard to believe that English is my first language!

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top