how do i get the file pointer in a ifstream object?

This is a discussion on how do i get the file pointer in a ifstream object? within the C++ Programming forums, part of the General Programming Boards category; hiya, i'd just like to get the pointer if it's possible because i need to load the data somewhere else, ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    how do i get the file pointer in a ifstream object?

    hiya,

    i'd just like to get the pointer if it's possible because i need to load the data somewhere else,

    ie,

    GetPointerFromFile(...)
    {
    ifstream fin("data.dat")
    fin.seekg(offset, ios::beg);
    return fin_pointer; // how?
    }

    GetData(fin_pointer)
    {
    fin_pointer.readdata....
    };

    my problem here is, if i read the data in GetPointerFromFile then i have to allocate a dynamic array of char then return it, but i wouldn't be able to delete it anymore and i also don't like to delete it somewhere else but on that function only,

    many thanks,

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What's wrong with passing an ifstream reference to the function for opening and positioning?
    Code:
    int get_file ( ifstream& fin, long offset )
    {
      fin.open ( "data.dat" );
      if ( fin.is_open() )
        return OPEN_FAILURE; // Or a suitable exception
    
      fin.seekg ( offset, ios::beg );
    
      return OPEN_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    err., i just need the pointer, because loading of the data will be handled by another function(not using ifstream anymore).. or maybe this isn't possible because loading the data can only be handled by the ifstream class??

    many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21