Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > read problem

Reply
Thread Tools

read problem

 
 
JasBascom
Guest
Posts: n/a
 
      02-24-2004
if i had a ifstream object

ifstream validdata;

then with the following code.

int list;

union Allrecords record[list]
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.



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-24-2004

"JasBascom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> if i had a ifstream object
>
> ifstream validdata;
>
> then with the following code.
>
> int list;
>
> union Allrecords record[list]
> 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


 
Reply With Quote
 
 
 
 
JasBascom
Guest
Posts: n/a
 
      02-24-2004
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?
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-25-2004

"JasBascom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
JasBascom
Guest
Posts: n/a
 
      02-25-2004
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.
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      02-25-2004
JasBascom wrote:
>
> 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.


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-25-2004

"JasBascom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Program to read from EOR end of last read? snoopy_@excite.com Java 3 04-07-2006 12:32 PM
Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access? Doug ASP .Net 3 11-04-2005 07:35 PM
Re: Unable to read video DVDs and can read Data DVDs Biz DVD Video 0 07-22-2005 03:44 AM
How much was read during istream::read ? Steve C++ 6 05-13-2004 05:54 PM
Re: How to change Read Only Constraint to Read-Write Isaac VHDL 0 07-10-2003 01:43 PM



Advertisments