📄 txttool.cpp
字号:
// TxtTool.cpp: implementation of the CTxtTool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TxtTool.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTxtTool::CTxtTool()
:m_bIsOpen(FALSE)
{
}
CTxtTool::~CTxtTool()
{
}
BOOL CTxtTool::Open(const CString &strTxtFile,
UINT nOpenFlags)
{
if (!CFileFind().FindFile(strTxtFile))
return FALSE;
m_strTxtFile = strTxtFile;
m_bIsOpen = m_File.Open(m_strTxtFile, CFile::modeRead|CFile::typeText);
return m_bIsOpen;
}
BOOL CTxtTool::DeleteLine(const CString &strLineSign)
{
ASSERT(m_bIsOpen);
if (!m_bIsOpen) return FALSE;
CStringArray arsFileContent;
CString strTemp;
while (m_File.ReadString(strTemp))
{
if (strTemp.Find(strLineSign) != -1)
continue;
arsFileContent.Add(strTemp);
}
m_File.Close();
if (!::DeleteFile(m_strTxtFile))
return FALSE;
return CreateTxtFile(m_strTxtFile, arsFileContent);
}
BOOL CTxtTool::DeleteLine(const CString &strStartLineSign,
const CString &strEndLineSign )
{
ASSERT(m_bIsOpen);
if (!m_bIsOpen) return FALSE;
CStringArray arsFileContent;
//--------------
BOOL bStart = FALSE;
CString strTemp;
while (m_File.ReadString(strTemp))
{
if (strTemp.Find(strStartLineSign) != -1)
bStart = TRUE;
if (bStart)
{
if (strTemp.Find(strEndLineSign) != -1)
bStart = FALSE;
continue;
}
arsFileContent.Add(strTemp);
}
m_File.Close();
if (!::DeleteFile(m_strTxtFile))
return FALSE;
//--------------
return CreateTxtFile(m_strTxtFile, arsFileContent);
}
BOOL CTxtTool::CreateTxtFile(const CString &strTxtFile,
const CStringArray &arsContent )
{
ASSERT(!strTxtFile.IsEmpty());
CStdioFile m_FileCreate;
if (m_FileCreate.Open(strTxtFile, CFile::modeCreate | CFile::modeWrite | CFile::typeText))
{
for (int i = 0; i < arsContent.GetSize(); i++)
{
m_FileCreate.WriteString(arsContent.GetAt(i) + "\n");
}
m_FileCreate.Close();
return TRUE;
}
return FALSE;
}
BOOL CTxtTool::ReplaceLineContent(const CString &strSource,
const CString &strGoal)
{
ASSERT(!strSource.IsEmpty());
CStringArray arsFileContent;
CString strTemp;
while (m_File.ReadString(strTemp))
{
strTemp.Replace(strSource, strGoal);
arsFileContent.Add(strTemp);
}
m_File.Close();
if (!::DeleteFile(m_strTxtFile))
return FALSE;
return CreateTxtFile(m_strTxtFile, arsFileContent);
}
BOOL CTxtTool::FindIsContainString(const CString &strSign)
{
if (strSign.IsEmpty())
return FALSE;
BOOL bResult = FALSE;
CString strTemp;
while (m_File.ReadString(strTemp))
{
if (strTemp.Find(strSign) != -1)
{
bResult = TRUE;
break;
}
}
m_File.Close();
return bResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -