txt格式文本分割
偶期末课程设计打算编一个把txt文本按800字节分割。。比如内容一共1000字节,就分成两个txt保存,第一个里面800字节,第二个里面是剩余的。
我参考着一个GG的程序编出来后运行,每次都是被强制关闭,让发送错误报告的那种 T T .. 但是文本诡异的被分好了。。
我贴下分割的代码。。麻烦各位帮我看下哪里有问题。。谢谢
// cut程序
int CFINAL_RDlg::cutstart()
{
CFile m_sourcefile;
CFileException ex;
CString buffer[1000];
int i=1,len;
CWaitCursor wait;
CFile m_outputfile; // 写文件
CString outputpath; //输出文件的路径
DWORD dwRead;
UINT nCount = 800;
UINT newlen = 800; //????
UpdateData(TRUE);
if (!m_path1.IsEmpty()) {
if (!m_sourcefile.Open(m_path1, CFile::modeRead | CFile::shareDenyNone, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
m_edit.SetFocus();
m_edit.SetSel(0, -1);
return 1;
} //这是撒么。。> <
len = m_sourcefile.GetLength();
}
else {
MessageBox(_T("请选择待分割的源文件."), _T("txt_cut"), MB_ICONEXCLAMATION);
return 1;
}
if (m_path2.IsEmpty()) {
MessageBox(_T("请选择分割后保存到的目标文件夹."), _T("txt_cut"), MB_ICONEXCLAMATION);
return 1;
}
if (len < 801) {
MessageBox(_T("文件长度为不足没有必要再进行分割."), _T("txt_cut"), MB_ICONEXCLAMATION);
m_sourcefile.Close();
m_path1 = _T("");
m_filename = _T("");
UpdateData(FALSE);
return 1;
}
//开始cut
CString tr_title; //输出文件的名称
CString tx=".txt"; //!!!!没有后缀名 .txt
do{
//命名
tr_title.Format ("%s%d%s",m_filename,i,tx);
//判断选择目录未尾是否已有"\"符
if(m_path2.Right(1)=='\\')
outputpath = m_path2;
else
outputpath = m_path2 + _T("\\");
outputpath+=tr_title;
if (!m_outputfile.Open(outputpath,
CFile::modeWrite |
CFile::shareExclusive |
CFile::modeCreate, &ex)) {
TCHAR szError[1024];
ex.GetErrorMessage(szError, 1024);
::AfxMessageBox(szError);
m_sourcefile.Close();
return 1; // mark 米懂
}
do {
dwRead = m_sourcefile.Read(buffer, nCount);
m_outputfile.Write(buffer, dwRead);
}//while size is less than 800
while (dwRead > 0 && m_outputfile.GetLength() < newlen); /// ????
m_outputfile.Close();
UpdateData(FALSE);
i++;
}
while (dwRead > 0);
// close source
m_sourcefile.Close();
m_path1 = _T("");
m_filename = _T("");
m_sourcefile.Close ;
UpdateData(FALSE);
return 0;
}