DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Thread: ifstream issue

  1. #1
    Sefi P Guest

    ifstream issue


    Hello,

    I am using a user-defined struct (not class).
    with ifstream::operator>> i am inserting data from a file into a pointer
    to this struct, (being the second argument to the >> operator) because the
    file states how many structs there are, and their full details. it is an
    array whose size depends on the file input.

    anyway, it does read and store everything into the array of structs. (the
    array is allocated with new, it is not an automatic object), but after returning
    the ifstream from the operator>>, the pointer obviously holds nothing.

    why?
    must i pass a "real" object to such operators?
    in past times, i used "real" objects as the second argument which wrote to
    their data memebers. however, this is not the case now.

    thank you,
    Sefi P.

  2. #2
    Sefi P Guest

    Re: ifstream issue



    Ok,

    something has gone terribly wrong.
    a very very simple passing pointer to a function, allocating new there, and
    returning - does not work!

    i don't know what is wrong, it is not connected to the ifstream, anyway.
    it is seemingly just fine.

    maybe the frigging MFC has to do with this conspiracy..

    sefip


  3. #3
    Sefi P Guest

    Re: ifstream issue



    Riddle solved - sorry, don't bother answering.

    it was simply a ** that was required, you can't pass * and "new" on it, you
    must pass pointer to this, and new on the content of the passed pointer.

    well, alas. this 'c' style is almost ancient history. haven't used it for
    ages. silly me! *hammers his head in the table*

    well, at least lessons re-rememebered. that counts to something (hardly something,
    though.. *sigh*)

    sefip.


  4. #4
    Danny Kalev Guest

    Re: ifstream issue



    Sefi P wrote:
    >
    > Riddle solved - sorry, don't bother answering.
    >
    > it was simply a ** that was required, you can't pass * and "new" on it, you
    > must pass pointer to this, and new on the content of the passed pointer.
    >
    > well, alas. this 'c' style is almost ancient history. haven't used it for
    > ages. silly me! *hammers his head in the table*
    >
    > well, at least lessons re-rememebered. that counts to something (hardly something,
    > though.. *sigh*)
    >
    > sefip.


    Boy, I wish more questions on this forum were answered by their
    posters:) It would save us some work...
    The ** trick is a common idiom when passing a pointer to a function that
    allocates the address for that pointer *and* binds storage to that
    address (in other words, when you want the function to allocate and
    assign an address). You can avoid the syntax mess by using *& instead:

    void allocate(void *& p, int size)
    {
    p = new char[size]; //should work
    }

    Danny

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links