Thread: most efficient way to write filestream objects?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    most efficient way to write filestream objects?

    I have a program where I have to create many different file stream objects to create and write to files. Is it more efficient/faster to create these objects in functions, where they will be created and closed when the functions are called? Or would it be better to find a way to create the file stream objects once and find a way to have functions call them?

    Or make a class to handle creating/closing file stream objects?

    Has anyone used managed C++ (.net)? What would be the most efficient way to create/close many file stream and stream writer objects?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have no idea about managed C++, so if that CHANGES what I say, then so be it. Someone with actual managed C++ experience may be able to tell.

    Generally, opening and closing a file is the SAFEST method, but not the most efficient, as opening and closing files is generally fairly slow - having one filestream that is passed from one function to the next is a much better option - just open the file in one place, and then use it in many functions, and close it when you know it's finished with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You could use <fstream>. All of the classes are there already.
    Is this an assignment where the idea is to manage your own file pointer or something?

    Anyway, along the <fstream> lines, you can use one stream to open many different files -- just one at a time. Or, you can have many fstream objects and maintain a container of pointers to them (like std::vector<std::fstream*>).

    *edit* it seems I misunderstood the question
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    57
    Thanks. So I'll try to open file streams maybe when the program is initialized, and then call them when needed (I have to figure out how to do this). Or make a class to take care of things, if I have the time. Sigh. So many things to do, so little time.

    Managed C++ steals a lot of stuff from C++, so it should work basically the same way. I hope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. How to write a program
    By Tashfique in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2008, 11:28 AM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. This should be the Cprogramming.com anthem!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-21-2002, 12:01 AM