read problem

Discussion in 'C++' started by JasBascom, Feb 24, 2004.

  1. JasBascom

    JasBascom Guest

    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.
     
    JasBascom, Feb 24, 2004
    #1
    1. Advertising

  2. "JasBascom" <> wrote in message
    news:...
    > 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
     
    John Harrison, Feb 24, 2004
    #2
    1. Advertising

  3. JasBascom

    JasBascom Guest

    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?
     
    JasBascom, Feb 24, 2004
    #3
  4. "JasBascom" <> wrote in message
    news:...
    > 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
     
    John Harrison, Feb 25, 2004
    #4
  5. JasBascom

    JasBascom Guest

    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.
     
    JasBascom, Feb 25, 2004
    #5
  6. 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
     
    Karl Heinz Buchegger, Feb 25, 2004
    #6
  7. "JasBascom" <> wrote in message
    news:...
    > 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
     
    John Harrison, Feb 25, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Isaac
    Replies:
    0
    Views:
    2,465
    Isaac
    Jul 10, 2003
  2. Doug
    Replies:
    3
    Views:
    4,379
    Bruce Barker
    Nov 4, 2005
  3. Replies:
    3
    Views:
    534
    John W. Krahn
    Apr 7, 2006
  4. Steve
    Replies:
    6
    Views:
    3,685
    Steve
    May 13, 2004
  5. lokb
    Replies:
    5
    Views:
    4,659
    Victor Bazarov
    Jul 8, 2004
Loading...

Share This Page