read problem

J

JasBascom

if i had a ifstream object

ifstream validdata;

then with the following code.

int list;

union Allrecords record

  • union Allrecords *rec = record;
    int offset = 1;
    int reccount

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

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

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

    this compiles ok, but when I try to execute I get a debug assertion error.
    in short I would like to be able to read rec from validdata.
 
J

John Harrison

JasBascom said:
if i had a ifstream object

ifstream validdata;

then with the following code.

int list;

union Allrecords record

  • union Allrecords *rec = record;
    int offset = 1;
    int reccount

    validdata.seekg(-offset, ios::end);


  • This is wrong, why are you seeking with an offset of -1? You want to seek to
    the end of the file, which is

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

    Again this is wrong, you want to seek to the beginning of the file, which is

    validdata.seekg(0, ios::beg);
    reccount = filesize/sizeof(Allrecords);
    rec = new(Allrecords[reccount]);

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

    this compiles ok, but when I try to execute I get a debug assertion error.
    in short I would like to be able to read rec from validdata.

    Rest seems OK, you were just using an offsets of 1 and -1 instead of 0 for
    some reason. I think I pointed this out before. If you don't agree or don't
    understand then its a good idea to respond, for instance it could be that
    you want to skip the first byte in the file, if that was the case then your
    code as written would be OK.

    If you check the value of the filesize variable against the size you know
    the file to be, you will see that it is one out. Its easy enough to do

    filesize = validdata.tellg();
    cout << "the file size is " << filesize << '\n';

    The other thing you should try an do is track down exactly where the debug
    assertion error is. Try removing all your code except the code you posted
    here. Do you still get the debug assertion error?

    john
 
J

JasBascom

thank you john for your help, and I'am rewriting the code in smaller chunk,
that is how I was able to find the problem in the first place. Without the read
line there isn't an error. The program compiles but doesn't execute, the error
said to check documentation for Visual studio C++. I don't have documentation
for it, I hear what you are saying about the variable offset and will change
it.
The only thing that I could possibly think is wrong with the read line is that
it requires a type conversion for char.
as in
validdata.read((char*) // requires type conversion as rec is the object of a
union, do you know of any type conversion i could use?
 
J

John Harrison

JasBascom said:
thank you john for your help, and I'am rewriting the code in smaller chunk,
that is how I was able to find the problem in the first place.

Glad to hear it. I read this after my reply to your later post. Apologies if
I sound tetchy, but I see so many newbies in this group going though what
you are going through because they approach programming in the wrong way. I
do admire your persistence.

john
 
J

JasBascom

the read problem is fixed with a simple ampersand & before rec. for anyone who
has the same problem in future, the char only wants an address to start reading
from.
 
K

Karl Heinz Buchegger

JasBascom said:
the read problem is fixed with a simple ampersand & before rec. for anyone who
has the same problem in future, the char only wants an address to start reading
from.

Since you rewrote the whole thing, you may now need the address of operator.
In the original code you posted, it is not needed and in fact would be
a serious bug. rec already is a pointer, and you don't want read to store
the read bytes in the pointer. Instead you want read to store the read
bytes where rec points to. rec has that address as its value, thus one would
pass rec just the way you did in your original code snippet.
 
J

John Harrison

JasBascom said:
the read problem is fixed with a simple ampersand & before rec. for anyone who
has the same problem in future, the char only wants an address to start reading
from.

That's not true of the code you posted.

In the code you posted rec was a pointer (i.e. it was already an address),
so & is not necessary.

If rec was an array you also would not need a & (although in this case it
wouldn't do any harm).

Only if rec is a normal variable is & necessary.

I think the moral is details matter in programming, which is why posters are
always advised to cut and paste real code when they post, rather than type
something which is only approximately what they really have.

john
 

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

Similar Threads

type conversion 5
std::sort whole program 2
Short program 3
write a union object using write 1
unions 2
Problem with file handling 5
built in types 4
Check if program finished to read a whole file 5

Members online

No members online now.

Forum statistics

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

Latest Threads

Top