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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post 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?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Martyj
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Apr 23, 2009 11:23 am
Location: Ogden Utah
Contact:

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

Post 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.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

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

Post 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?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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();
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

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

Post by Scoody »

short0014 wrote:Is this the correct way to use the destructor?
Yes, you use the desctructor to clean up after yourself.
Post Reply