Resource Management - Files

This is a discussion on Resource Management - Files within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm just reading up on resource management and RAII (Resource Acquisition Is Initialisation) and it got me thinking regarding ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Resource Management - Files

    Hi,

    I'm just reading up on resource management and RAII (Resource Acquisition Is Initialisation) and it got me thinking regarding files.

    If say I have a program that uses different file names. Would it be a good idea to create a file_handle class that simply takes a file name as an argument and the file is opened in the constructor and closed in the destructor?

    I can then use objects of this type either as member objects of other classes or within functions, that way when the objects go out of scope the file is released. Would this be something that works in real world code?

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Yes, but the class you're talking about already exists: ifstream and ofstream.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    aah okay. So ifstream and ofstream open the files and then close them when they go out of scope?

    I have used them previously, I didn't realise they automatically closed the files though.

    Thanks,

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by darren78 View Post
    aah okay. So ifstream and ofstream open the files and then close them when they go out of scope?

    I have used them previously, I didn't realise they automatically closed the files though.

    Thanks,
    I don't know (or can't think) of any containers in the STL that does not clean up after itself. For files, that means closing them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Cheers LL. What sort of resources do I need to be looking at within constructors/destructors then? I know of memory allocated on the heap. As a beginner, are there any others that I should be aware of?

    Thanks again.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    STL pretty much takes care of everything.
    For memory, there is unique_ptr and shared_ptr. For other resources, there is unique_ptr and shared_ptr with custom deleters.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by darren78
    What sort of resources do I need to be looking at within constructors/destructors then? I know of memory allocated on the heap.
    Database and prepared statement handles are another example, e.g., I have written my own RAII wrappers for the SQLite C interface (and there are other C++ wrappers out there with the same idea).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You sure shared_ptr with custom deleter won't cut it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    So when acquiring additional memory, if I use the smart pointers I'm not restricted to allocating and deallocating memory in constructors/destructors? I can just use the smart pointers inside a scope and when it goes out of scope the memory is released?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Yes, they are RAII and what you say is the purpose of RAII.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    Database and prepared statement handles are another example, e.g., I have written my own RAII wrappers for the SQLite C interface (and there are other C++ wrappers out there with the same idea).
    I did something with C# and SQL for a university project a few months ago and was a little unsure regarding database opening/closing.

    Should I open the connection, do whatever needs to be done immediately and then close immediately, or is it acceptable to open the connection at the beginning of a programs execution and leave it open until the program closes?

    The program I wrote was for an e-commerce website and when the customer browsed the products, the DB was queried and products displayed, information retrieved etc on stock levels, descriptions etc?

    Should the DB be opened and closed with each query or is this too much overhead?

    Thanks.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by Elysia
    You sure shared_ptr with custom deleter won't cut it?
    That depends on whether RAII is the only thing that you are trying to cut

    Quote Originally Posted by darren78
    So when acquiring additional memory, if I use the smart pointers I'm not restricted to allocating and deallocating memory in constructors/destructors?
    The idea is that if you use tools that provide you with RAII functionality, then you do not need to implement RAII yourself.

    Quote Originally Posted by darren78
    I can just use the smart pointers inside a scope and when it goes out of scope the memory is released?
    Yes. This is the basic idea of RAII: the associate the lifetime of a resource with the lifetime of an object.

    Quote Originally Posted by darren78
    Should the DB be opened and closed with each query or is this too much overhead?
    It depends. If you are waiting for user input, then there may be little point in maintaining the database connection, unless there is only one user. But if you are going to execute a series of queries, opening and closing the database connection for each of them is obviously silly.
    Last edited by laserlight; 08-14-2010 at 01:20 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Yes, they are RAII and what you say is the purpose of RAII.
    Okay, cheers. The book i'm using only really touches briefly on auto_ptr and advocates the use of constructors/destructors for aquiring resources, etc.

    It's good to know that I am not limited to that.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    That depends on whether RAII is the only thing that you are trying to cut


    The idea is that if you use tools that provide you with RAII functionality, then you do not need to implement RAII yourself.


    Yes. This is the basic idea of RAII: the associate the lifetime of a resource with the lifetime of an object.
    Thanks again. It's becoming a little clearer now!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  2. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  3. Multiple Resource files
    By doodlak in forum C++ Programming
    Replies: 0
    Last Post: 07-04-2002, 06:13 PM
  4. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 08:35 AM
  5. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 08:45 AM

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