Results 1 to 10 of 10

Thread: iostream using pointer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163

    iostream using pointer

    typedef TArray<CShape*> CShapes;
    typedef TArrayIterator<CShape*> CShapesIterator;

    CShapes *shapes = new CShapes(10, 0, 10);


    void TDrawWindow::save()
    {

    ofstream os("c:\\abc.txt");
    CShapesIterator i(*shapes );

    while (i) {
    CShape *p = i++;

    os<<p;
    }

    }

    is it correct or not?
    if it is correct then how to retrive it from file

    I have done so but it is not working.


    void TDrawWindow::Open()
    {
    ifstream is("c:\\abc.txt");


    shapes ->Flush();

    unsigned numPoints;

    is >> numPoints;

    while (*numPoints--) {
    CShape shape;
    is >> shape;
    shapes->Add(&shape);
    }

    Invalidate();

    }
    Purushottam

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, not correct. You're only saving the pointer value.
    Unless there is a specific overload of << for TObject * or TShape *. If not you'll have to do
    os << *i;
    and overload the << operator for const TShape &.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    Thanks



    friend ostream& operator<< ( ostream& os, CShape& shape );
    ostream& operator<< ( ostream& s, CLine& dt )
    {
    s << shape .pt1 << shape .pt2 << '\n';;
    return s;
    }

    this is what I have done for direct access.
    But How to overload for pointer
    Purushottam

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by purusingh
    Thanks



    friend ostream& operator<< ( ostream& os, CShape& shape );
    ostream& operator<< ( ostream& s, CLine& dt )
    {
    s << shape .pt1 << shape .pt2 << '\n';;
    return s;
    }

    this is what I have done for direct access.
    But How to overload for pointer
    You dont have to overload for pointers. Just dereference p.

    Z.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    please show me how to use it in these functions


    void TDrawWindow::save()
    {

    ofstream os("c:\\abc.txt");
    CShapesIterator i(*shapes );

    while (i) {
    CShape *p = i++;

    os<<p;
    }




    void TDrawWindow::Open()
    {
    ifstream is("c:\\abc.txt");


    shapes ->Flush();

    unsigned numPoints;

    is >> numPoints;

    while (*numPoints--) {
    CShape shape;
    is >> shape;
    shapes->Add(&shape);
    }

    Invalidate();

    }
    Purushottam

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Didn't I already?
    Code:
    while (i) {
      CShape *p = i++;
      os<<*p;
    }
    And make the prototype of the << operator
    ostream& operator<< ( ostream& s, const CLine& dt);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Posts
    163
    yes I did so but it is not working.
    Purushottam

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    In that case my knowledge fails.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    In your Load function, you read the number of shapes saved. You never save that value in your save function.

    Z.

  10. #10
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Also, while(*numPoints--) should be while(numPoints--), since it isnt a pointer value.

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width