Thread: Strange file problem.

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    53

    Strange file problem.

    Hey everyone. I'm trying to create a logger for my program so that I can find very easily where the bugs occurs, what functions fail... but the problem is that my logger is failing, wich is pretty annoying since its purpose was to help me fix bugs... the problem is that it doesn't print ANYTHING to the file... but I verified and fstream::is_open() returns true... I also verified the string to output (with MessageBox) and its content is exactly what I wanted it to be... could someone tell me where I'm wrong ? This bug is really killing me since I really don't see anything that could lead to an error...
    Code:
    struct Log {
    	static void CreateLog();
    	static void Release();
    	static void Write(std::string, bool);
    	static void FormatString(std::string&, const char*, const char*, const UINT);
    	static std::fstream file;
    };
    
    void Log::CreateLog() {
    	file.open("log.txt");
    }
    
    void Log::Release() {
    	file.close();
    }
    
    void Log::Write(std::string argument, bool result) {
    	std::ostringstream oss;
    	oss << argument << "... " << (result ? "successful" : "failed") << '\n';
    	argument = oss.str();
    	MessageBox(0,argument.c_str(),0,0);
    	file << argument;
    }
    
    void Log::FormatString(std::string& buf, const char* argument, const char* file, const UINT line) {
    	std::ostringstream oss;
    	oss << argument << " in " << file << " on line " << line;
    	buf = oss.str();
    }
    Code:
    std::string argument;
    Log::FormatString(argument, "Creating Gdiplus::Pen object", __FILE__, __LINE__);
    Log::Write(argument, (m_Pen = new Pen(Color(0,0,0))) != NULL);
    
    Log::FormatString(argument, "Creating Gdiplus::Graphics object", __FILE__, __LINE__);
    Log::Write(argument, (m_Graphics = new Graphics(hdc)) != NULL);
    Thank you all.
    Last edited by tilex; 12-28-2004 at 10:14 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > file << argument;
    You could try following this with
    file.flush();
    to make sure your data really is in the file, and not in some internal buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM