[SOLVED] C++: File just won't open

Started by
3 comments, last by F1N1TY 16 years, 10 months ago
Hello all, I'm making a log-file class. Basically, the requirements for my class are: non-singleton, labeled output (reason for proxy class), don't want to pass references to instances everywhere, overloaded << operator. Basically, my class supports all of that, but I'm having an issue. The file won't open! I ran it through the visual studio debugger, and everything seems to check out fine. Please note, that the problem does lie within the file not OPENING (any problems with closing, I'll figure out later.) Here's the source of the problem:

std::map< std::string, boost::shared_ptr< std::ofstream > > logger::files;			// Filenames to their corresponding filestreams

logger::logger(const std::string &f_name ) : title("UNDEFINED"), 
											 file_name(f_name)

{
	// First we need to get the filestream
	std::string lc_file_name(f_name);	// Our lowercase version
	std::transform(lc_file_name.begin(), lc_file_name.end(), lc_file_name.begin(), std::tolower );
		
	if ( files.find(lc_file_name) == files.end() )
	{
		std::ofstream* temp = new std::ofstream(f_name.c_str(), std::ios::out | std::ios::trunc | std::ios::app);
		boost::shared_ptr< std::ofstream > p(temp);
		MessageBox(0, L"we're inside the if statement...", L"test", MB_OK);
		files[lc_file_name] = p;
	}
	
	file_stream = files[lc_file_name]; 

	//file_stream->open(f_name.c_str(), std::ios::out | std::ios::trunc | std::ios::app);
	if(file_stream->is_open())
		MessageBox(0, L"The file is open!", L"NONE", MB_OK);
}


Here's the rest of the source: LOG.H:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <boost/shared_ptr.hpp>
#include <map>
#include <cctype>
#include <ctime>
//#include </* stuff for time collection*/>

namespace log
{

	class logger;

	// PROXY-CLASS:  Buffer, for multiple appends on a single line.
	class proxy
	{
	public:
		proxy( logger &lf )	{ this->lf = &lf; }
		~proxy(){}

		template<typename T>
		proxy& operator<<(const T &t)
		{
			*lf->file_stream << t;			
			return *this;
		}

	private:
		logger* lf;
	};

	class logger
	{
	public:

		friend class proxy;

		logger(const std::string &file_name );
		~logger();
		
		void set_title(const std::string &title);
		void clear();		
		void write(int rep, char letter);				// For appending more than 1 of the same char
		
		template<typename T>
		inline proxy error(const T &t)
		{
			*file_stream << "E " << time_to_string() << " [" << title << "]:  <ERROR> " << t; 	
			return proxy(*this);
		}

		template<typename T>
		inline proxy operator<<(const T &t)
		{
			*file_stream << "  " << time_to_string() << " [" << title << "]:  " << t;	
			return proxy(*this);
		}
		
	private:

		std::string time_to_string();

		static std::map< std::string, boost::shared_ptr< std::ofstream > > files;			// Filenames to their corresponding filestreams
		
		std::string file_name;
		std::string title;
		boost::shared_ptr< std::ofstream > file_stream;
	};	
}


LOG.CPP

#include "log.h"
#include <windows.h>

namespace log
{

std::map< std::string, boost::shared_ptr< std::ofstream > > logger::files;			// Filenames to their corresponding filestreams

logger::logger(const std::string &f_name ) : title("UNDEFINED"), 
											 file_name(f_name)

{
	// First we need to get the filestream
	std::string lc_file_name(f_name);	// Our lowercase version
	std::transform(lc_file_name.begin(), lc_file_name.end(), lc_file_name.begin(), std::tolower );
		
	if ( files.find(lc_file_name) == files.end() )
	{
		std::ofstream* temp = new std::ofstream(f_name.c_str(), std::ios::out | std::ios::trunc | std::ios::app);
		boost::shared_ptr< std::ofstream > p(temp);
		MessageBox(0, L"we're inside the if statement...", L"test", MB_OK);
		files[lc_file_name] = p;
	}
	
	file_stream = files[lc_file_name]; 

	//file_stream->open(f_name.c_str(), std::ios::out | std::ios::trunc | std::ios::app);
	if(file_stream->is_open())
		MessageBox(0, L"The file is open!", L"NONE", MB_OK);
}

	logger::~logger() { /* NO CLEANUP NEEDED; HANDLED BY SHARED_PTR DTOR */ }

	void logger::set_title( const std::string &title ) 
	{
		this->title = title; 
	}

	void logger::clear()
	{
		file_stream->close();
		file_stream->open(file_name.c_str(), std::ios::out | std::ios::trunc | std::ios::app );		// This file actually clears the log
	}

	void logger::write(int rep, char letter)
	{
		std::string temp_string("");

		temp_string.insert(temp_string.begin(), rep, letter);
		*file_stream << "  " <<  time_to_string() << " [" << title << "]:  " << temp_string << std::endl;
	}

	std::string logger::time_to_string()
	{
		// Get's the current date/time and formats it to this format:
		// HH:MM:SS  MM-DD-YY

		return "BLAH";
	}

}


main.cpp

#include <windows.h>

#include "log.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{

	log::logger lf("TEST.txt");
	lf.set_title("Tester");

	lf << "Yeah!";
	lf << "This...";
	lf << "Is...";
	lf << "Just...";
	lf << "A...";
	lf << "test...";
	
	return (0);
}


[Edited by - F1N1TY on June 2, 2007 12:08:46 AM]
"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo
Advertisement
What happens when you run it outside visual studio?

Also, insure the filename is exactally "test.txt" inside the *same*
directory as the programs *.exe.

The code itself looks fine to me.
The code should create the test.txt document (if I'm correct), if it doesn't exist (in this case, it doesn't exist).

I'm not able to get the "MessageBox(0, L"The file is open!", L"NONE", MB_OK);" line to run at all... the is_open is either evaluating to false, or I'm doing something wrong :-
Been bugging me all day, can't think of it for the life of me...

"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo
Change
std::ofstream* temp = new std::ofstream(f_name.c_str(), std::ios::out | std::ios::trunc | std::ios::app);

to:
std::ofstream* temp = new std::ofstream(f_name.c_str(), std::ios::out | std::ios::trunc);


This should create the file.
THANK YOU!

THANK YOU!

THANK YOU!

You saved me so hardcore on this... I've been looking at this code for three days, and I knew it would be something simple. I just can't believe it was that :P

+Rating, thank you so much Crypter!
"I'd rather know one thing, no matter how ordinary, than discourse endlessly on great issues." -- Galileo

This topic is closed to new replies.

Advertisement