CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2002
    Posts
    23

    Delete File associated with fstream

    Hi,

    In my program i have an fstream that creates a file "myfile.txt" in a directory. ie.

    fstream file;
    file.open("myfile.txt", ios:: out | ios::i n);

    I was just wondering if there was anyway to delete that file "myfile.txt" from the directory within my program? I dont want it to exist after the program finishes executing.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I've done something similar. Are you familiar with the "Resource
    Acquisition Is Initialization" programming paradigm? What it
    basically means in C++ is that you acquire a resource as part of
    an object's construction and you relinquish the resource as a part
    of that objects destruction. So, what you can do is have a
    wrapper class called TempFile or something like that. The
    constructor of TempFile would open the file you need. TempFile
    would also provide a method to get at the output file object; you
    could even leave the ofstream object public if you wanted.
    Then, the destructor of TempFile would delete the path referenced
    by the oftream object [after closing it, of course].

    Is this description adequate?

  3. #3
    Join Date
    Nov 2002
    Posts
    23
    Hi Paul,
    I think I understand what your saying, however i'm still unclear as to what the C++ syntax would be to do something like "destructor of TempFile would delete the path referenced
    by the oftream object [after closing it, of course]." ie. what is the syntax to delete the path referenced by the ofstream?...I cant seem to find anything like that in the ofstream methods.

    thanks

  4. #4
    Join Date
    Apr 1999
    Posts
    3,585
    How about using a plain old "remove()" function?
    Gort...Klaatu, Barada Nikto!

  5. #5
    Join Date
    Jan 2001
    Posts
    588
    There may be a better way to do this.

    Store the path of the filename in a std::string inside your TempFile class. Write your destructor like this:

    Code:
    // file = ofstream object
    // fileName = file path
    ~Tempfile()
    {
         if (file.is_open()) file.close();
         remove(fileName);
    }
    I don't know if there's a less-C way to do it, without using remove(), but that should do the job.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Lil'Hasher
    Hi Paul,
    I think I understand what your saying, however i'm still unclear as to what the C++ syntax would be to do something like "destructor of TempFile would delete the path referenced
    by the oftream object [after closing it, of course]." ie. what is the syntax to delete the path referenced by the ofstream?...I cant seem to find anything like that in the ofstream methods.
    thanks
    Code:
    #include <fstream>
    #include <string>
    
    class CFoo
    {
    public:
      CFoo(std::string strFile)
      {
        m_strFile = strFile;
    
        // Open file
        m_ofstrFile.open(m_strFile);
      }
    
      ~CFoo()
      {
        // Close file
        m_ofstrFile.close();
    
        // Delete file
        remove(m_strFile);
      }
      
    private:
      std::string   m_strFile;
      std::ofstream m_ofstrFile;
    };

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

Featured