#### Archived

This topic is now archived and is closed to further replies.

# Const parameter problem (probably easy)...

This topic is 5888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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<
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

##### Share on other sites
//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;
//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();
};

}

int LOGFILE::CreateLogFile(const char* FileName)
}

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

//If this doesn''t work, I''m sorry for wasting time

##### Share on other sites
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

##### Share on other sites
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");

##### Share on other sites
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

##### Share on other sites

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..

##### Share on other sites
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