   Write to log file
  Hi im kinda new to c++, im making a MFC application, but i got problems getting it to write to a file.Im using visual c++ 6.0 (with service pack 5)anyways, i want it to write in log file "Server closed by user exit" when you exit the program.Source is like this:UpdaterDLG.CPP:void CUpdaterDlg::OnClose() {  int nResult;  nResult = MessageBox("Are you sure you want to exit?", "Quit?",  MB_YESNO | MB_ICONQUESTION);  if (nResult == IDYES)  ofstream f("logfile.txt", ios::out | ios::app);  f << "Server closed by user exit" << endl;  f.close(); EndDialog(0);}Updater.Hprivate:ofstream f;without the code in updater.h it will not compile.In StdAfx.h :#include <iostream>#include <conio.h>#include <winsock2.h>#include <stdio.h>#include <fcntl.h>#include <io.h>#include <stdlib.h>#include <fstream.h>#include <string.h>i know i might overdrive a bit by all the includes but i need them later :DAnyways, like this is compiles ok, but, it writes nothing to the file when i click the exit buttom.Anyone got any ideas of why ?    [edited by - Bmsfx on February 10, 2004 10:28:24 PM][edited by - Bmsfx on February 10, 2004 10:31:34 PM]
  It makes it difficult when you do not post the error. Guessing, I''d hazard that there is some conflict because you use both old-style headers (fstream.h) along with standard headers (iostream).   Colin Jeanne | Invader''s Realm;  How about addingusing namespace std;  after the includes, and replacing string.h and fstream.h with string and fstream? The .h files of those headers are deprecated and should be no longer used.Toolmaker   -Earth is 98% full. Please delete anybody you can. ;quote:Original post by Toolmaker How about addingusing namespace std;  after the includes, and replacing string.h and fstream.h with string and fstream? The .h files of those headers are deprecated and should be no longer used.   If you can't use old style header files, ie string.h or fstream.h, you have to use the new style, which is cstring and cfstream.  I'm pretty sure that is just on my compiler (MSVC++.net), but that's how I have to do it...I also believe that is the new ANSI/ISO standard...Dan... [edited by - xdanillumx on February 11, 2004 3:45:41 AM] [edited by - xdanillumx on February 11, 2004 3:47:05 AM] [edited by - xdanillumx on February 11, 2004 3:48:14 AM] [edited by - xdanillumx on February 11, 2004 3:50:15 AM];quote:Original post by Invader X  It makes it difficult when you do not post the error. Guessing, I''d hazard that there is some conflict because you use both old-style headers (fstream.h) along with standard headers (iostream).   Colin Jeanne | Invader''s Realm but i dont get any errors, it just dont write anything in the file...--------------------Configuration: Updater - Win32 Debug--------------------Compiling...StdAfx.cppCompiling...Updater.cppUpdaterDlg.cppGenerating Code...Linking...Creating browse info file...Updater.exe - 0 error(s), 0 warning(s)as for the other guys, i tryed both removing iostream.h and change the stdafx.h file to this:// stdafx.h : include file for standard system include files,//  or project specific include files that are used frequently, but// are changed infrequently//#if !defined(AFX_STDAFX_H__B77976AD_86A2_4F4C_9409_75629B1CE585__INCLUDED_)#define AFX_STDAFX_H__B77976AD_86A2_4F4C_9409_75629B1CE585__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#define VC_EXTRALEAN// Exclude rarely-used stuff from Windows headers#include <afxwin.h>    // MFC core and standard components#include <afxext.h>    // MFC extensions#include <afxdisp.h>   // MFC Automation classes#include <afxdtctl.h>// MFC support for Internet Explorer 4 Common Controls#ifndef _AFX_NO_AFXCMN_SUPPORT#include <afxcmn.h>// MFC support for Windows Common Controls#endif // _AFX_NO_AFXCMN_SUPPORT#include <conio.h>#include <winsock2.h>#include <stdio.h>#include <fcntl.h>#include <io.h>#include <stdlib.h>#include <string>#include <fstream>using namespace std;//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__B77976AD_86A2_4F4C_9409_75629B1CE585__INCLUDED_)it still dont write anything to the file, no errors compiling also. ;  Im using microsoft visual c++ 6.0 enterprise edition with all options installed.The program is a MFC appwizard.exe project.and just on a side note, i could get it to work easy in a win32 console application, but since i need the program as a grafics interface program that doesnt help me much.   [edited by - Bmsfx on February 11, 2004 5:12:26 AM];  How about:ofstream f("logfile.txt", ios::out | ios::app);if (f) // Check if f is valid{    f << "Server closed by user exit" << endl;    f.close();}  This way you are sure the file is opened. Assuming the file is open isn''t the safest way.Toolmaker   -Earth is 98% full. Please delete anybody you can. ;  well i tryed that still dont write anything...anyways, i get this when i debug the exe from the lineofstream f("logfile.txt", ios::out | ios::app);i do get a error there that say this:std::basic_ios >CXX0030: Error : Expression cannot be evaluateton the other side (watch 1) it sayofstreamCXX0017: Error: symbol ofstream not found.also i notet this...when i run the program and click exit buttom, it will exit if i press yes, or no (normally no woudnt exit the program) so something is messed up ;  okay fixed the exit buttom stuff, had to put code somewhere else.....still dont write anything tho i think ill need a cup o coffee before proceeding...   [edited by - Bmsfx on February 11, 2004 8:54:26 AM];  1) remove the ''ofstream f;'' in your private member of your class declaration2) try in CUpdaterDlg::onClose() ofstream f("file.txt", ios::out | ios::app);if(f.fail()){  // failed to open the file}f << "write some text" << endl;f.close();hope that helpsMatt 