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

📄 reader.cpp

📁 一个DB文件读取工具,最终转换CSV文件的代码.. 很有参考价值 作者:tanis
💻 CPP
字号:
// Reader.cpp: implementation of the CReader class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Reader.h"
#include "DBReader.h"
#include "DBReader.h"
#include <algorithm>
#include "DBReaderDlg.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

const int CReader::WM_WTNOTIFY = WM_USER + 18;

BOOL CReader::CWorkThread::InitInstance()
{
	return TRUE;
}

int CReader::CWorkThread::Run()
{
    CFile fileSource;
	if (!DoSourceFile())
	{
		AfxMessageBox(_T("文件读取失败!"));
		SetState(_T("文件读取失败!"));
		return -1;
	}
	SetState(_T("文件读取成功!"));
	AfxMessageBox(_T("文件读取成功!"));

	if (!DoSavedFile())
	{
		AfxMessageBox(_T("文件保存失败!"));
		SetState(_T("文件保存失败!"));
		return -1;
	}
	AfxMessageBox(_T("处理成功!"));
	SetState(_T("处理成功!"));

	return 0;
}

void CReader::CWorkThread::DestroyDatas()
{
	for (int i = 0; i < m_vctRecord.size(); i ++)
	{
		delete m_vctRecord[i];
	}
	m_vctRecord.clear();
	if (mDbf != NULL)
	{
		delete mDbf;
		mDbf = NULL;
	}
}

BOOL CReader::CWorkThread::DoSourceFile()
{
	BOOL bRes = FALSE;
	int iTotalCount = 0;
	int iCurCount = 0;
	SetState(_T("开始处理......"));
	SetProgress(0.0f);
	SendNotifyMsg();
	CString sState;
	try
	{
		if ( mDbf->Open(m_sSourceFile,0))
		{
			iTotalCount = mDbf->mRecNum;
			for (int i = 0; i < mDbf->mRecNum; i ++)
			{
				iCurCount ++;
				BOOL bDel;
				CRecord *precord = new CRecord;
				mDbf->SeekRec (i);
				mDbf->ReadRec (precord,&bDel);
				if (bDel)
				{
					delete precord;
					precord = NULL;
					continue;
				}
				m_vctRecord.push_back(precord);
				sState.Format(_T("正在处理第%d帧,共%d帧"),iCurCount,iTotalCount);
				SetState(sState);
				SetProgress((float)((float)iCurCount / (float)iTotalCount));
				SendNotifyMsg();
			}
			SetProgress(1.0f);
			SendNotifyMsg();
			bRes = TRUE;
		}
		
	}
	catch (...)
	{
		
	}

	if (mDbf->mFile.m_hFile != CFile::hFileNull)
	{
		mDbf->Close();
	}

	return bRes;
}

BOOL CReader::CWorkThread::DoSavedFile()
{
	BOOL bRes = FALSE;
	int iTotalCount = 0;
	int iCurCount = 0;
	CStdioFile file;
	CString sState;
	SetProgress(0.0f);
	SetState(_T("开始保存文件......"));
	SendNotifyMsg();
	try
	{
		if(CDBReaderDlg::IsFileExists(m_sSavedFile))
		{
			::DeleteFile(m_sSavedFile);
		}
		if (file.Open(m_sSavedFile,CFile::modeCreate|CFile::modeWrite))
		{
			iTotalCount = m_vctRecord.size();
			if (m_pWndNotify->MessageBox(_T("是否排顺序,可能十分耗时?"), "询问", MB_YESNO) == IDYES)
			{
				SetState(_T("开始排序......"));
				SendNotifyMsg();
				std::sort(m_vctRecord.begin(),m_vctRecord.end(),CWorkThread::SortByFirst);
			}		
	       	SetState(_T("开始保存文件......"));
	    	SendNotifyMsg();
        	for (int i = 0; i < iTotalCount; i ++)
			{
				file.WriteString(m_vctRecord.at(i)->ToString());
				sState.Format(_T("正在处理第%d帧,共%d帧"),iCurCount,iTotalCount);
				SetState(sState);
				SetProgress((float)((float)iCurCount / (float)iTotalCount));
			
			}
			SetProgress(1.0f);
			SendNotifyMsg();
			bRes = TRUE;
		}

	}
	catch (...)
	{
		
	}

	if (file.m_hFile !=  CFile::hFileNull)
	{
		file.Close();
	}

	return bRes;

}

void CReader::CWorkThread::SendNotifyMsg()
{
	if (m_pWndNotify != NULL && m_pWndNotify->m_hWnd != NULL)
	{
		TNotifyMsg * pNewMsg = new TNotifyMsg(m_sStateString,m_fProgress);
		m_pWndNotify->SendMessage(WM_WTNOTIFY,(WPARAM)pNewMsg,(LPARAM)0);
	}
}

CReader::CReader(CWnd * tpNotifyWnd,CString tsFileSource,CString tsFileSaved)
{
	m_ctworker.m_bAutoDelete = FALSE;
	m_ctworker.SetNotifyWnd(tpNotifyWnd);
	m_ctworker.SetSourceFilePath(tsFileSource);
	m_ctworker.SetSavedtFilePath(tsFileSaved);
}

CReader::~CReader()
{
}

void CReader::DoWork()
{
	m_ctworker.CreateThread(0,1024*2);
}


⌨️ 快捷键说明

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