Const parameter problem (probably easy)...

Started by
5 comments, last by Moe 22 years, 3 months ago
I have written a nice little class (with some functions) for handling output to a file for logging purposes. There is one problem with this. I recently tried to modify it so that I could specify the filename of the file that I wanted to open and write to (instead of having it hard-coded in). Here is the class, complete with the function definitions (I know this looks like a lot of code but its really not): The class...
      
//return types for the scripting

const int LOGFILE_OK		= 0;
const int LOGFILE_BAD		= 1;

//the logfile class - creates a file and logs all the information on 

//how the program behaves to it

class LOGFILE
{
public:
	~LOGFILE();
	ofstream outfile;

	int CreateLogFile(const char* FileName);
	int LogtoFile(char Outputstring[256]);
	void CloseLogFile();

	
};
  
  
The functions...
       
//--------------------------------------------------//

// Name: ~LOGFILE()

// Desc: destructor that closes the logfile

//--------------------------------------------------//

LOGFILE::~LOGFILE()
{
	//close the file

	outfile.close();

}

//--------------------------------------------------//

// Name: CreateLogFile()

// Desc: function that creates a file called log.dat 

//--------------------------------------------------//

int LOGFILE::CreateLogFile(const char* FileName)
{
	//create the file and test if it was made ok

	outfile.open(FileName);

	if(!outfile)
	{
		return LOGFILE_BAD;
	}

	//return ok

	return LOGFILE_OK;
};

//--------------------------------------------------//

// Name: LogtoFile()

// Desc: function that puts a string to the file 

//--------------------------------------------------//

int LOGFILE::LogtoFile(char OutputString[256])
{
	//output it to the file and flush it so it doesn't

	//get lost if the program messes up

	outfile<<OutputString;
	outfile.flush();

	//return ok

	return LOGFILE_OK;
}

//--------------------------------------------------//

// Name: LogtoFile()

// Desc: function that puts a string to the file 

//--------------------------------------------------//

void LOGFILE::CloseLogFile()
{
	//close the file

	outfile.close();

}

  
Now the problem is that I want to be able to specify the file by doing something like:
      
LOGFILE d3dlogfile.
d3dlogfile.CreateLogFile("system//data.log");

      
Now how exactly would I go about modifying it so that it works properly? Moe's site Edited by - Moe on January 8, 2002 8:46:03 PM
Advertisement
//I have added somecode that might help, but you will have to make a few other changes just to
//get the program running smoothly.

//return types for the scripting
const int LOGFILE_OK = 0;
const int LOGFILE_BAD = 1;
//the logfile class - creates a file and logs all the information on
//how the program behaves to it
class LOGFILE
{private: const char* filename; //Add
public:
~LOGFILE();
ofstream outfile;
int CreateLogFile(const char* FileName);
int LogtoFile(char Outputstring[256]);
void CloseLogFile();
void vCreateLogFile(const char* FileName); //Add
};


void LOGFILE::vCreateLogFile(const char*FileName) //Add
{filename=FileName; //Add
}

int LOGFILE::CreateLogFile(const char* FileName)
{//Your code;
return filename; //Add
}


LOGFILE d3dlogfile.
d3dlogfile.vCreateLogFile("system//data.log"); //Correct

//If this doesn''t work, I''m sorry for wasting time
Ah, thanks. I will have to give it a try.

So is there any way to pass a parameter as const? I am just curious to know if there would be a better way of doing it.

Moe''s site
It is const. Not sure what your problem is. The code you''ve posted will work as shown. Have you tried compiling it and have gotten errors?

This is fine:
void Foo (const char *stg) { ... }
Foo ("constant string");
It compiles fine, but for some stupid reason when I go to open the file, ofstream fails. Thats the only reason I can think of why it is failing.

Moe''s site


Is the filename "system//data.log" correct? Does the subdirectory "system" exist off the current working directory? Does it contain the file "data.log"?

Are you programming for Unix? Linux? If not, then you might want to change your filename to either "system/data.log" OR "system\\data.log".

Just a guess..
Ok, it looks like it was a bit of a directory problem. Here's the way it was set up:

Prototype
- Prototype
- Debug (the exe was in here, not the base directory)
- System

It looks like MSVC++ created a directory inside the one I had and it didn't have the 'system' folder within it. Thanks!

Moe's site

Edited by - Moe on January 9, 2002 12:14:16 AM

This topic is closed to new replies.

Advertisement