admin 发表于 2022-7-20 21:43:16

检测查找文件是否失败并提示

// StockNotifyNewsCheck.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "StockNotifyNewsCheck.h"
#include "StockNotifyNewsCheckDlg.h"
#include <direct.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

char gszSystemDir;                        // 系统路径
char gszIniFileName;                        // 主配置文件路径
/////////////////////////////////////////////////////////////////////////////
// CStockNotifyNewsCheckApp

void LogFile(char *String)
{
    try
    {
                char szPath;
                memset(szPath, 0, MAX_PATH);
                szPath = 0;
                _snprintf(szPath, MAX_PATH-1, "%s\\Log", gszSystemDir);
                _mkdir(szPath);

                CFile        TmpFile;
      CString FileName;
      SYSTEMTIME SysTime;
      ::GetLocalTime(&SysTime);
      CTime curTime = CTime::GetCurrentTime();
      FileName.Format("%s\\Log\\NotifyNewsCheck%04d%02d%02d_%02d.log",
            gszSystemDir,
            curTime.GetYear(),
            curTime.GetMonth(),
            curTime.GetDay(),
                        curTime.GetHour());
      
      if(!TmpFile.Open(FileName,
            CFile :: typeBinary | CFile :: shareDenyNone |CFile :: modeWrite
            | CFile::modeCreate | CFile::modeNoTruncate,
            NULL))
      {
            return;
      }
      
      char TmpString;
      
      _snprintf(TmpString, 2559, "%02d:%02d:%02d:%04d%s\r\n",
            curTime.GetHour(),
            curTime.GetMinute(),
            curTime.GetSecond(),
            SysTime.wMilliseconds,
            String);
      TmpFile.SeekToEnd();
      TmpFile.Write(TmpString, strlen(TmpString));
      TmpFile.Close();
    }
    catch(...)
    {
      CString strLogErr = _T("");
      int err = ::GetLastError();
      strLogErr.Format("err:%d Content:%s ", err, String);
    }
}

void LogFile_Name(char *LogFileName, char *String)
{
    try
    {
                char szPath;
                memset(szPath, 0, MAX_PATH);
                szPath = 0;
                _snprintf(szPath, MAX_PATH-1, "%s\\Log", gszSystemDir);
                _mkdir(szPath);
               
                CFile        TmpFile;
      CString FileName;
      SYSTEMTIME SysTime;
      ::GetLocalTime(&SysTime);
      CTime CurTime = CTime::GetCurrentTime();
      FileName.Format("%s\\Log\\%s.log",
            gszSystemDir,
            LogFileName);
      
      if(!TmpFile.Open(FileName,
            CFile :: typeBinary | CFile :: shareDenyNone |CFile :: modeWrite
            | CFile::modeCreate | CFile::modeNoTruncate,
            NULL))
      {
            return;
      }
      
      char TmpString;
      
      _snprintf(TmpString, 2559, "%s\r\n",String);
      TmpFile.SeekToEnd();
      TmpFile.Write(TmpString, strlen(TmpString));
      TmpFile.Close();
    }
    catch(...)
    {
      CString strLogErr = _T("");
      int err = ::GetLastError();
      strLogErr.Format("err:%d Content:%s ", err, String);
    }
}

BEGIN_MESSAGE_MAP(CStockNotifyNewsCheckApp, CWinApp)
        //{{AFX_MSG_MAP(CStockNotifyNewsCheckApp)
                // NOTE - the ClassWizard will add and remove mapping macros here.
                //    DO NOT EDIT what you see in these blocks of generated code!
        //}}AFX_MSG
        ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStockNotifyNewsCheckApp construction

CStockNotifyNewsCheckApp::CStockNotifyNewsCheckApp()
{
        // TODO: add construction code here,
        // Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CStockNotifyNewsCheckApp object

CStockNotifyNewsCheckApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CStockNotifyNewsCheckApp initialization

BOOL CStockNotifyNewsCheckApp::InitInstance()
{
        AfxEnableControlContainer();

        // Standard initialization
        // If you are not using these features and wish to reduce the size
        //of your final executable, you should remove from the following
        //the specific initialization routines you do not need.

#ifdef _AFXDLL
        Enable3dControls();                        // Call this when using MFC in a shared DLL
#else
        Enable3dControlsStatic();        // Call this when linking to MFC statically
#endif

        //
        // 初始化程序路径
        //
        char szFileName;
        szFileName = 0;
        memset(gszSystemDir, 0, MAX_PATH);
        ::GetModuleFileName(NULL, gszSystemDir, MAX_PATH-1);
        char *pDest = strrchr(gszSystemDir, '\\');
        if (pDest == NULL)
        {
                return FALSE;
        }
        *pDest = '\0';
        memset(gszIniFileName, 0, MAX_PATH);
        _snprintf(gszIniFileName, MAX_PATH-1, "%s\\config.ini", gszSystemDir);

        CStockNotifyNewsCheckDlg dlg;
        m_pMainWnd = &dlg;
        int nResponse = dlg.DoModal();
        if (nResponse == IDOK)
        {
                // TODO: Place code here to handle when the dialog is
                //dismissed with OK
        }
        else if (nResponse == IDCANCEL)
        {
                // TODO: Place code here to handle when the dialog is
                //dismissed with Cancel
        }

        // Since the dialog has been closed, return FALSE so that we exit the
        //application, rather than start the application's message pump.
        return FALSE;
}
页: [1]
查看完整版本: 检测查找文件是否失败并提示