在網上下了一部小說,大概3M左右,非常的長,想把它分爲很多個部分。
後來聽說網上有專門的軟件可以分割小說。
於是去下了一個,然後用了一下,非常方便。
後來我注意到,他分割的方法就是檢測每一行,如果一行裏面同時包含了‘第’和‘章’兩個字,就會重新創建一個txt文件,從而讓每一章都是一個單獨的txt文件
觀察完功能之後,於是打算自己寫一個
首先是做界面
第一個框是需要切割的源文件
第二個目標路徑代表最後輸出的切割完畢文件存放的地址。
然後兩個瀏覽按鈕,分別是打開對話框和文件夾對話框
切割的時候,用CStdioFile這個類
一行一行的讀入txt文件。然後用str.Find去查找關鍵字
檢測到之後便創建新文件。
開始切割按鈕代碼:
void CNovelCutDlg::OnStartCut()
{
// TODO: Add your control notification handler code here
int count=0;
CString s1,OutPath;
s1.Format("%04d.txt",count);
OutPath=m_OutPath+s1;
CFile ofile(OutPath,CFile::modeWrite|CFile::modeCreate);
CStdioFile file(m_InPath,CFile::modeRead);
bool flag=true;
CString str;
while(file.ReadString(str))
{
int pos1=str.Find("第");
int pos2=str.Find("章");
str=str+"\r"+"\n";
if(pos1!=-1&&pos2!=-1)
{
if(flag)
{
ofile.Close();
s1.Format("%04d.txt",count);
OutPath=m_OutPath+s1;
if(ofile.Open(OutPath,CFile::modeWrite|CFile::modeCreate))
{
int len=str.GetLength();
ofile.Write(str.GetBuffer(len),len);
}
count++;
flag=false;
}
else
{
ofile.Close();
count++;
s1.Format("%04d.txt",count);
OutPath=m_OutPath+s1;
if(ofile.Open(OutPath,CFile::modeWrite|CFile::modeCreate))
{
int len=str.GetLength();
ofile.Write(str.GetBuffer(len),len);
}
}
}
else
{
int len=str.GetLength();
ofile.Write(str.GetBuffer(len),len);
flag=false;
}
}
MessageBox("剪切完成");
}
兩個瀏覽按鈕的代碼:
void CNovelCutDlg::OnInBtn()
{
// TODO: Add your control notification handler code here
CString str=("Text File (*.txt)|*.txt||");
CFileDialog Open(true,NULL,NULL,NULL,str,NULL);
if(IDOK==Open.DoModal())
{
m_InPath=Open.GetPathName();
CString name=Open.GetFileName();
SetDlgItemText(IDC_IN_PATH,m_InPath);
int len=m_InPath.GetLength()-name.GetLength();
SetDlgItemText(IDC_OUT_PATH,m_InPath.Left(len));
}
}
void CNovelCutDlg::OnOutBtn()
{
// TODO: Add your control notification handler code here
BROWSEINFO bi;
::ZeroMemory(&bi,sizeof(bi));
LPITEMIDLIST target=SHBrowseForFolder(&bi);
if(target!=NULL)
{
char targetpath[MAX_PATH];
SHGetPathFromIDList(target,targetpath);
m_OutPath.Format("%s\\",targetpath);
SetDlgItemText(IDC_OUT_PATH,m_OutPath);
}
}