⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writelogfile.cpp

📁 更新GPS FIRMWARE 查詢GPS FIRMWARE版本
💻 CPP
字号:
#include "stdafx.h"


#define COUNT_WRITE_READ_BUFFER_SIZE 16

//===============function prototype===================================
void WriteToLogFile(TCHAR szFileName[],TCHAR szContext[] );
void CreateLogFile(TCHAR szFileName[]);
void WriteCountToLogFile(TCHAR szResetCountFile[],DWORD dwNewResetCount);
DWORD ReadCountFromLogFile(TCHAR szResetCountFile[]);
//------------------------------------------------------------------


//=====CreateLogFile====================================
//author:Justin Wei
//purpose: always create a new file , and its file name is according to the parameter		   
//usage notice :
//				
//---------------------------------------------------------------

void CreateLogFile(TCHAR szFileName[])
{
	DWORD dwTextLen=0;	
	HANDLE hFile=NULL;
	TCHAR szErrMsg[256];


	hFile = CreateFile(		szFileName,
                            GENERIC_READ|GENERIC_WRITE,
                            0,			   //object cannot be shared
                            NULL,
                            OPEN_ALWAYS, //Opens the file, if it exists. If the file does not exist, the function creates the file
                            0,
                            NULL
					   );
	

    if(INVALID_HANDLE_VALUE ==hFile||NULL==hFile)
	{
		wsprintf(szErrMsg,TEXT("create %s fail"),szFileName);
		MessageBox(NULL,szErrMsg,TEXT("create file fail"),MB_OK);
	
	}

    //=======close file handle=======================
    if(!CloseHandle(hFile)) //close file 
    {
		wsprintf(szErrMsg,TEXT("close %s handle error "),szFileName);
		MessageBox(NULL,szErrMsg,TEXT("close fail"),MB_OK);
	}
    //-------------------------------------------------------------
	

}



//=====WriteToLogFile=============================================================
//author:Justin Wei
//purpose: write szContext to szFileName,
//		   the szContext will be appended to the end of this file
//usage notice :Before call this function , 
//				user must make sure the szFileName has already be created
//--------------------------------------------------------------------------------
void WriteToLogFile(TCHAR szFileName[],TCHAR szContext[] )
{
	DWORD dwTextLen=0,dwByte,dwRet;
	TCHAR szErrMsg[256];
	HANDLE hFile;
	char * pcWriteBuf;

	hFile = CreateFile( szFileName,
                            GENERIC_READ|GENERIC_WRITE,
                            0,			   //object cannot be shared
                            NULL,
                            OPEN_EXISTING,    //open existing
                            0,
                            NULL);
                            
    if(!hFile)
    {
    	MessageBox(NULL,TEXT("open log file error "),TEXT("open fail"),MB_OK);
    }
    			
	//--move the cursor to the end of the file , and then write(append) the message we want to write to file
	//======================================================================================
	SetFilePointer(hFile,0,NULL,FILE_END);//move the cursor to the end of the file
	dwTextLen=_tcslen(szContext);	//caculate the length ;
    //-------------------------------------------------------------------

    pcWriteBuf=(char*)malloc(dwTextLen*sizeof(char));

	WideCharToMultiByte (CP_ACP, 0, szContext, -1, pcWriteBuf, dwTextLen, NULL, NULL);	

	dwRet = WriteFile(	hFile, 
                   		pcWriteBuf,//szContext, 
		           		dwTextLen, 
                   		&dwByte, 0
                   	 );
    if(dwRet)
    {
		if(dwByte != dwTextLen)
	    {
	        memset(szErrMsg,0,sizeof(szErrMsg));
	        _stprintf(szErrMsg,L"Error. want to Write %d,real Write %d\r\n",dwTextLen,dwByte);
			MessageBox(NULL,szErrMsg,TEXT("Write ERROR"),MB_OK);
	    }

    }
    else//cannot write
    {
    	MessageBox(NULL,TEXT("Write Log file error"),TEXT("Error"),MB_OK);
    }
    //----------------------------------------------------------------------------
 
    
    //==========close file handle===================================================================
    FlushFileBuffers( hFile );
    if(!CloseHandle(hFile)) //close file 
    {
		MessageBox(NULL,TEXT("close log file error "),TEXT("close fail"),MB_OK);
	}
    //-------------------------------------------------------------

  	//MessageBox(NULL,TEXT("finish write"),TEXT("finish write"),MB_OK);

	free(pcWriteBuf);	
}




void WriteCountToLogFile(TCHAR szCountFile[],DWORD dwNewCount)
{
	DWORD dwTextLen=COUNT_WRITE_READ_BUFFER_SIZE;  
	DWORD dwByte,dwRet;
	TCHAR szErrMsg[256];
	HANDLE hFile;
	char * pcWriteBuf;

	hFile = CreateFile(     szCountFile,
                            GENERIC_READ|GENERIC_WRITE,
                            0,			   //object cannot be shared
                            NULL,
                            OPEN_EXISTING,    //open existing
                            0,
                            NULL
					   );
                            
    if(!hFile)
    {
    	wsprintf(szErrMsg,TEXT("open %s  file error "),szCountFile);
		MessageBox(NULL,szErrMsg,TEXT("open fail"),MB_OK);
    }
    			

	
	pcWriteBuf=(char*)malloc(dwTextLen*sizeof(char));

	memset(pcWriteBuf,0,dwTextLen);   
	_itoa( (int) dwNewCount , pcWriteBuf, 10 );



	dwRet = WriteFile(	hFile, 
                   		pcWriteBuf, 
		           		dwTextLen,  
                   		&dwByte, 0
                   	 );
    if(dwRet)
    {
		if(dwByte != dwTextLen)
	    {
	        memset(szErrMsg,0,sizeof(szErrMsg));
	        _stprintf(szErrMsg,L"Error. in %s , want to Write %d,real Write %d\r\n",szCountFile,dwTextLen,dwByte);
			MessageBox(NULL,szErrMsg,TEXT("Write ERROR"),MB_OK);
	    }

    }
    else//cannot write
    {
    	wsprintf(szErrMsg,TEXT("Write %s file error"),szCountFile);
		MessageBox(NULL,szErrMsg,TEXT("Error"),MB_OK);
    }
    //----------------------------------------------------------------------------
 
    
    //==========close file handle===================================================================
    FlushFileBuffers( hFile );
    if(!CloseHandle(hFile)) //close file 
    {
		wsprintf(szErrMsg,TEXT("close %s log file error"),szCountFile);
		MessageBox(NULL,szCountFile,TEXT("close fail"),MB_OK);
	}
    //-------------------------------------------------------------

	free(pcWriteBuf);	

}



DWORD ReadCountFromLogFile(TCHAR szCountFile[])
{
	DWORD dwTextLen=COUNT_WRITE_READ_BUFFER_SIZE;     
	DWORD dwByte,dwRet;
	TCHAR szErrMsg[256];
	HANDLE hFile;
	char * pcReadBuf;
	DWORD dwResetCountRecord=0;

	hFile = CreateFile(     szCountFile,
                            GENERIC_READ|GENERIC_WRITE,
                            0,			   //object cannot be shared
                            NULL,
                            OPEN_EXISTING,    //open existing
                            0,
                            NULL
					   );
                            
    if(!hFile)
    {
		wsprintf(szErrMsg,TEXT("open %s error. "),szCountFile);
    	MessageBox(NULL,szErrMsg,TEXT("open fail"),MB_OK);
    }
    				
	pcReadBuf=(char*)malloc(dwTextLen*sizeof(char));
    
	dwRet = ReadFile(	hFile, 
                   		pcReadBuf, 
		           		dwTextLen,  
                   		&dwByte, 0
                   	 );
    if(dwRet)
    {
		if(dwByte != dwTextLen)
	    {
	        memset(szErrMsg,0,sizeof(szErrMsg));
	        _stprintf(szErrMsg,L"Error.in %s, want to Read %d,real read %d\r\n",szCountFile,dwTextLen,dwByte);
			MessageBox(NULL,szErrMsg,TEXT("Read ERROR"),MB_OK);
	    }
		dwResetCountRecord=atoi(  (const char * )pcReadBuf );
    }
    else//cannot read
    {
    	wsprintf(szErrMsg,TEXT("read %s error"),szCountFile);
		MessageBox(NULL,szErrMsg,TEXT("Error"),MB_OK);
    }
    //----------------------------------------------------------------------------

    //==========close file handle===================================================================	
    if(!CloseHandle(hFile)) //close file 
    {
		wsprintf(szErrMsg,TEXT("close %s error "),szCountFile);
		MessageBox(NULL,szErrMsg,TEXT("close fail"),MB_OK);
	}
    //-------------------------------------------------------------

  	

	free(pcReadBuf);	

	return dwResetCountRecord;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -