文本块的操作

来源:互联网 发布:java中do while的用法 编辑:程序博客网 时间:2024/05/22 06:07

 

文本按块进行保存:

TXT文件中,是类似[RECT=1>第一段<1=RECT]这样的分块

 

 TXT文件中 分块号码升序

如:

[RECT=1>第一段<1=RECT]
[RECT=2>第2段<2=RECT]
[RECT=3>第三段<3=RECT]
[RECT=4>第四段<4=RECT]

 

问题: 当加入新块时, 如何使得文档仍然有序

 

解决算法:

 

 IF(文件未被创建)    则创建此文件,直接将此分块写入 ELSE(文件已经被创建), {       IF (不存在更小分块情况 )// 说明此分块最小      {        不存在相等分块情况 if (!bExistSmaller&&!bExistEqual) //说明此分块最小,且未被保存过            存在更大分块、不存在更大分块  的处理方法相同:先保存此最小分块,再保存文档中原先的分块即可                    存在相等分块情况if (!bExistSmaller&&bExistEqual) // 说明此分块最小,且曾被保存过            存在更大分块  if (bExistLarger)  以minLarger分块的起始点为分界线,先保存此分块内容,再保存minLarger及之后的分块            不存在更大分块 if (!bExistLarger) //仅有这一个分块   只需将修改内容保存在文本中即可      }   ELSE    {    存在更小分块情况  if (bExistSmaller)          不存在更大分块            存在相等的分块  if (!bExistLarger&&bExistEqual) //说明此分块最大,且曾被保存过              原修改文档分为两部分 此最大分块之前的内容  和此最大分块的内容              先将之前的存入文档,再将修改后的存入文档            不存在相等的分块 if (!bExistLarger&&!bExistEqual) //说明此分块最大,且未被保存过                直接将此分块添加到文件末尾        存在更大的分块             存在相等分块   if (bExistLarger&&bExistSmaller&&bExistEqual)  //说明此分块 既不最大也不最小, 且曾被保存过                 先保存此分块之前的,再保存此分块,最后保存此分块之后的             不存在相等分块 if (bExistLarger&&bExistSmaller&&!bExistEqual) //说明此分块 既不最大也不最小, 且未被保存过                 先保存MinLarger之前的分块,再保存此分块,最后保存minLarger及之后的分块   } } 


 

 

代码:

 

void SaveFenKuaiContentToTxtFile(int nTobeSaved,CString modifiedContent,CString filePath){//获得原文件中的号码信息GetRectNumFormFile(m_List,filePath);//比它大且仅大一个位置的分块int minLarger=INT_MAX; //  比nSelect大 且 仅大一个位置//  下面循环作用:  确定 将要保存的分块号nSelect和已经保存的分块号之间的关系//  是否存在比它大的分块//  是否存在比它小的分块//  是否存在与之相等的分块BOOL bExistLarger=FALSE;//  是否存在比nSelect大的   若不存在,则说明nSelect是已经保存分块中 分块号码最大的BOOL bExistSmaller=FALSE;BOOL bExistEqual=FALSE;POSITION pos=m_List.GetHeadPosition();ITEMINFO item;for (int i=0;i<m_List.GetCount();i++){item=m_List.GetNext(pos);// 有比它大的分块// 循环结束后,找到比它仅大一个位置的分块if (item.value>nTobeSaved&&item.value<=minLarger){bExistLarger=TRUE;minLarger=item.value;continue;}//有比它小的分块if (item.value<nTobeSaved){bExistSmaller=TRUE;continue;}//有与它相等的分块if (item.value==nTobeSaved){bExistEqual=TRUE;continue;}}CString  fileLeft;CString  fileMid;CString  fileRight;CStdioFile wfile;  CFileFind fileFind;  // 源文件中 分块号码升序// 将一新分块插入源文件中,此分块可能被保存过,也可能未被保存过// 若被保存过,则更新源文件  若为保存过 则插入// 新文件仍是按升序排放//  IF(文件未被创建)//     则创建此文件,直接将此分块写入//  ELSE(文件已经被创建),//  {//     //    IF (不存在更小分块情况 )// 说明此分块最小//       {//         不存在相等分块情况 if (!bExistSmaller&&!bExistEqual) //说明此分块最小,且未被保存过//             存在更大分块、不存在更大分块  的处理方法相同:先保存此最小分块,再保存文档中原先的分块即可//             //         存在相等分块情况if (!bExistSmaller&&bExistEqual) // 说明此分块最小,且曾被保存过//             存在更大分块  if (bExistLarger)  以minLarger分块的起始点为分界线,先保存此分块内容,再保存minLarger及之后的分块//             不存在更大分块 if (!bExistLarger) //仅有这一个分块   只需将修改内容保存在文本中即可//       }    //    ELSE//     {//     存在更小分块情况 if (bExistSmaller)  //         不存在更大分块//             存在相等的分块 if (!bExistLarger&&bExistEqual) //说明此分块最大,且曾被保存过//              原修改文档分为两部分 此最大分块之前的内容  和此最大分块的内容//               先将之前的存入文档,再将修改后的存入文档//             不存在相等的分块 if (!bExistLarger&&!bExistEqual) //说明此分块最大,且未被保存过//                 直接将此分块添加到文件末尾//         存在更大的分块//              存在相等分块   if (bExistLarger&&bExistSmaller&&bExistEqual)  //说明此分块 既不最大也不最小, 且曾被保存过//                  先保存此分块之前的,再保存此分块,最后保存此分块之后的////              不存在相等分块 if (bExistLarger&&bExistSmaller&&!bExistEqual) //说明此分块 既不最大也不最小, 且未被保存过//                  先保存MinLarger之前的分块,再保存此分块,最后保存minLarger及之后的分块//    }//  }if(!fileFind.FindFile(filePath)) // 查找文件是否存在   {  if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Close();fileFind.Close();return;}else {  // 文件存在// 将文件转换为CString 字符串CString fileContent;txtFileToCString(filePath,fileContent);//////////////////////////////////////////////////////////////////////////if (!bExistSmaller&&!bExistEqual) // 说明此分块号 最小且没有被保存过{//  将此分块修改的内容 保存为 str1 //  将txt中保存的修改内容读取出来, 保存为str2//  写入,先将st1写入,再将str2写入if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.WriteString(L"\r\n");wfile.SeekToEnd();wfile.Write(fileContent.GetBuffer(),fileContent.GetLength()*2);fileContent.ReleaseBuffer();wfile.Close();fileFind.Close();return;}if (!bExistSmaller&&bExistEqual) // 说明此分块最小,且曾被保存过{if (!bExistLarger) //仅有这一个分块   只需将修改内容保存在文本中即可{if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Close();fileFind.Close();return;}else{ //还有别的分块//以minLarger分块的起始点为分界线//fileLeft曾被保存的分块号, fileRight minLarger及minLarger以后的分块号 TwoPartsTxt(fileContent,minLarger,fileLeft,fileRight);if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();//--------------------------添加修改的分块CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();//--------------------------//添加minLarger及minLarger之后的分块wfile.WriteString(L"\r\n");wfile.SeekToEnd();wfile.Write(fileRight.GetBuffer(),fileRight.GetLength()*2);fileRight.ReleaseBuffer();wfile.Close();fileFind.Close();return;}}if (bExistSmaller)  //  说明此分块不是最小分块  分块数目大于1   如果 !bExistSmaller&&!bExistLarger  说明只有一个分块 用之前的两个条件语句即可解决,无需再重新执行一次{if (!bExistLarger&&!bExistEqual) //说明此分块最大,且未被保存过{// 直接将修改过的内容添加到文档最后if (!wfile.Open(filePath,CFile::modeReadWrite|CFile::modeNoTruncate|CFile::typeBinary))  {AfxMessageBox(L"无法打开修改过的文档");return;}WORD sign;  wfile.SeekToBegin();  wfile.Read(&sign,2);  if (sign!=0xfeff)  {  AfxMessageBox(L"不是UNICODE文档");  return;  } wfile.SeekToEnd(); wfile.WriteString(L"\r\n");wfile.SeekToEnd(); //--------------------------添加修改的分块CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();//--------------------------添加修改的分块wfile.Close();fileFind.Close();return;}if (!bExistLarger&&bExistEqual) //说明此分块最大,且曾被保存过{// 原修改文档分为两部分 此最大分块之前的内容  和此最大分块的内容// 先将之前的存入文档,再将修改后的存入文档TwoPartsTxt(fileContent,nTobeSaved,fileLeft,fileRight);if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();wfile.Write(fileLeft.GetBuffer(),fileLeft.GetLength()*2);fileRight.ReleaseBuffer();//wfile.WriteString(L"\r\n");wfile.SeekToEnd();//--------------------------添加修改的分块CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();//--------------------------添加修改的分块wfile.Close();fileFind.Close();return;}}if (bExistLarger&&bExistSmaller&&!bExistEqual) //说明此分块 既不最大也不最小, 且未被保存过{// 以minLarger为分界线,分成两部分 LEFT  RIGHT // 将修改后的文档 作为第三部分midTwoPartsTxt(fileContent,minLarger,fileLeft,fileRight);if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();wfile.Write(fileLeft.GetBuffer(),fileLeft.GetLength()*2);fileRight.ReleaseBuffer();wfile.SeekToEnd();//--------------------------添加修改的分块CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();//--------------------------添加修改的分块wfile.WriteString(L"\r\n");wfile.SeekToEnd();wfile.Write(fileRight.GetBuffer(),fileRight.GetLength()*2);fileRight.ReleaseBuffer();wfile.Close();fileFind.Close();return;}if (bExistLarger&&bExistSmaller&&bExistEqual) //说明此分块 既不最大也不最小, 且曾被保存过{// fileLeft为正确的 fileRight不正确 因为包含分块nTobeSavedTwoPartsTxt(fileContent,nTobeSaved,fileLeft,fileRight);CString temp;//  fileRight 为正确的 TwoPartsTxt(fileContent,minLarger,temp,fileRight);if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必须以二进制打开   return ;  WORD sign=0xfeff;  wfile.Write(&sign,2);  wfile.SeekToEnd();wfile.Write(fileLeft.GetBuffer(),fileLeft.GetLength()*2);fileRight.ReleaseBuffer();wfile.SeekToEnd();//--------------------------添加修改的分块CString str;str.Format(L"[RECT=%d>",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wfile.Write(modifiedContent.GetBuffer(),modifiedContent.GetLength()*2);modifiedContent.ReleaseBuffer();str.Format(L"<%d=RECT]",nTobeSaved);wfile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();//--------------------------添加修改的分块wfile.WriteString(L"\r\n");wfile.SeekToEnd();wfile.Write(fileRight.GetBuffer(),fileRight.GetLength()*2);fileRight.ReleaseBuffer();wfile.Close();fileFind.Close();return;}}}


 

 

原创粉丝点击