Page 1 of 1

destructor for file i/o, correct way to use?

Posted: Sun May 31, 2009 11:16 pm
by short
Hey all, quick question. I never really learned how to use a destructor before, but I think I figured it out.

I wrote a log class file for ease of creating a log of any errors for debugging. It looks like this:

Log.h

Code: Select all

#include <string>
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

class Log
{
private:
	 const char * filename;
	 ofstream file;

public:
	 Log(const char * filename);
	 ~Log();

	 void writeToFile(const char * text);

};
and

Log.cpp

Code: Select all

#include "Log.h"

Log::Log(const char * filename)
{
	 this->filename = filename;
	 this->file.open(this->filename);
}

Log::~Log()
{
	this->file.close();
}

void Log::writeToFile(const char * text)
{
	 this->file << text;

}
Is this the correct way to use the destructor?

Re: destructor for file i/o, correct way to use?

Posted: Mon Jun 01, 2009 1:03 am
by Martyj
I believe you only have to close files when you are witting to files. Otherwise it closes when the program closes. The way that I've seen it used is filename.close();

example:

Code: Select all

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Taken from www.cplusplus.com

http://www.cplusplus.com/doc/tutorial/files/

I don't know much about c++ though. I'm still a beginner.

Re: destructor for file i/o, correct way to use?

Posted: Mon Jun 01, 2009 4:32 am
by short
The way I envisioned it was when you call the constructor it opens the file, then you call writeToFile() over and over, and when the program exits it calls the deconstructor automatically closing the file.

Is that how it works?

Re: destructor for file i/o, correct way to use?

Posted: Mon Jun 01, 2009 4:38 am
by programmerinprogress
Depends, if the object isn't a pointer, then I guess the object just gets destroyed, thus closing the stream, if it is a pointer to a dynamically allocated object, then you have to have corresponding new and delete keywords (the delete keyword would go in the deconstructor)

I don't know if that answered your question, but I hope it did.

EDIT: just incase you didn't know, never call the constructor explicitly (i.e. in your code), object can destroy itself when it either falls out of scope or is deleted.

Re: destructor for file i/o, correct way to use?

Posted: Mon Jun 01, 2009 12:04 pm
by MarauderIIC
The way that you have it set up, one of two things can happen:
1) You have an instance of "Log" in something whose lifetime is as long as that of your program, thus the file is opened and truncated once at the start of the program, written to through the whole life of the program, and then closed when the program ends through the destructor. For instance, Log might be a singleton or a member of something that is a singleton.

Code: Select all

void someFn() {
    Log log = Log::getInstance();
}
or

Code: Select all

Log log;
void main() {
}
2) You declare an instance of "Log" where you need it. This means that every time you make another instance, the file is re-opened and then closed when Log goes out of scope (like at the end of the function). This means that, since you're just using .open(), your file will be truncated. You would need to call it with ios::app for one of the parameters so that you don't lose your old log data. Unfortunately, this means that you would need to truncate the file when your program starts, or regularly delete the file (otherwise you append to the data from the previous execution). You would need to do it something like this:

Code: Select all

void main() {
    ifstream mylogfile;
    mylogfile.open("mylog.txt"); //truncate the file
    mylogfile.close();
    while (go) {
        //heartbeat (aka main game loop)
    }
}
Where

Code: Select all

Log::Log(const char* filename) {
    this->filename = filename;
    this->file.open(this->filename, ios::app);
}

Log::~Log() {
    this->file.close();
}

Re: destructor for file i/o, correct way to use?

Posted: Tue Jun 02, 2009 2:10 am
by Scoody
short0014 wrote:Is this the correct way to use the destructor?
Yes, you use the desctructor to clean up after yourself.