Strange file problem.

This is a discussion on Strange file problem. within the C++ Programming forums, part of the General Programming Boards category; Hey everyone. I'm trying to create a logger for my program so that I can find very easily where the ...

  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 09:14 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > 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, 09: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, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21