首页 > 学院 > 开发设计 > 正文

文章标题

2019-11-08 20:04:17
字体:
来源:转载
供稿:网友

OperationFile.h // COperationFile.h : header file //

PRagma once

include

include

include

using namespace std;

class COperationFile { // Construction public: COperationFile(); ~COperationFile();

public: void OnFileFilling(In CString strFilePath , In BOOL bIsAddress=TRUE); /* 函数功能:删除重复的字符串(最简单的删除方法,有时间修改为二分法排序删除) * vctStrSaveFileData:输入输出参数 */ void deleteRepeatString(Inout vector& vctStrSaveFileData); void SetFileReadWrite( In CString cstrFileFullPath ); string CString2string(CString &strSrc); /* 函数功能:处理输入字符串,去掉与命令无关的信息 * strSrc:输入源字符串 * strReqHeader:“问”字符串 * bIsAns:是否为“回”字符串 * 返回值:处理后的字符串,使用vector的原因是一些回答命令会超过7个字符,这个时候需要把超过7个字符的命令拆分为不能大于7个字符的命令串 */ vector DealString(In CString strSrc, In CString strReqHeader, BOOL bIsAns=FALSE); };

OperationFile.cpp // COperationFile.cpp : implementation file //

include “stdafx.h”

include “OperationFile.h”

define MAX_STRING_COUNT 400

define Ans _T(“Ans:”)

define REQ _T(“Req:”)

COperationFile::COperationFile( ) { }

COperationFile::~COperationFile( ) { }

void COperationFile::OnFileFilling(In CString strFilePath, In BOOL bIsAddress/=TRUE/) { // TODO: Add your control notification handler code here int nRet=0; CString strReq=_T(“”); vector vctDealString; vector vctStrSaveFileData;

CStdioFile stdioFile;CFileException e;nRet = stdioFile.Open(strFilePath, CFile::modeNoTruncate | CFile::modeReadWrite | CFile::shareDenyNone, &e );if (!nRet){ AfxMessageBox(_T("文件打开失败!!!")); return;}stdioFile.SeekToBegin();CString strTemp;while (stdioFile.ReadString(strTemp)){ if (-1 != strTemp.Find(_T("Req:")) ) { vctDealString = DealString(strTemp,_T("")); strReq=vctDealString[0]; continue; } if (-1 == strTemp.Find(_T("Ans:")) ) { continue; } if (strReq.GetLength() > 0 ) { vctDealString = DealString(strTemp, strReq.Mid(0, 14), TRUE); vctStrSaveFileData.push_back(strReq); for (int k=0; k < vctDealString.size(); k++) { vctStrSaveFileData.push_back(vctDealString[k]); } }}stdioFile.Flush();stdioFile.Close();deleteRepeatString(vctStrSaveFileData);strFilePath+=_T(".txt");CStdioFile newFile;newFile.Open(strFilePath,CFile::modeCreate | CFile::modeWrite , &e );if (!nRet){ AfxMessageBox(_T("文件创建失败!!!")); return;}for (int k = 0; k < vctStrSaveFileData.size(); k++){ if ( !vctStrSaveFileData[k].IsEmpty() ) { newFile.WriteString(vctStrSaveFileData[k]); newFile.WriteString(_T("/r/n")); if ( vctStrSaveFileData[k+1].Find(_T("Req:")) != -1 ) { newFile.WriteString(_T("/r/n")); } } }newFile.Flush();newFile.Close();AfxMessageBox(_T("文件处理完成"));

}

vector COperationFile::DealString(In CString strSrc, In CString strReqHeader, BOOL bIsAns/=FALSE/) { int nValidCmd; vector vctStrSaveCmd; CString strTemp=strSrc, strAnsHeader=_T(“”), strAnsBody=_T(“”); CString x20x09=_T(” Receive”); if (!bIsAns) strTemp=strTemp.Mid(0, strTemp.Find(x20x09));

ifdef _DEBUG0

if (strTemp.Find(_T("19 02 8F")) != -1 ){ AfxMessageBox(_T("发现要找的字符串!!!"));}

endif

strTemp.Delete(5, 5);strAnsHeader=strTemp.Mid(0, 12);strAnsHeader.Insert(5, _T(" "));strAnsBody=strTemp.Mid(12, strTemp.GetLength());nValidCmd = strAnsBody.GetLength()/3;if (nValidCmd <= 7){ for (int k = 0; k < 7- nValidCmd; k++) { strAnsBody+= _T("00 "); } strAnsHeader.AppendFormat(_T("08 %02d "), nValidCmd); vctStrSaveCmd.push_back( strAnsHeader +strAnsBody);}else{ int nRemainder=0, nCounter=21, nQuotient; nRemainder = (nValidCmd-6)%7; nQuotient=(nValidCmd-6)/7; if (nRemainder>0) { nRemainder=7-nRemainder; nQuotient+=1; } for (int k = 0; k < nRemainder; k++) { strAnsBody+=_T("00 "); } strTemp.Format(_T("08 1%01X %02X "), nValidCmd&0xF00, nValidCmd&0xFF); vctStrSaveCmd.push_back( strAnsHeader+strTemp+strAnsBody.Mid(0, 18)); vctStrSaveCmd.push_back(strReqHeader+_T("08 30 00 00 00 00 00 00 00")); strAnsHeader.SetAt(4, '1'); strAnsHeader.SetAt(5, 'N'); strAnsBody=strAnsBody.Mid(18); //21 22 23 24... for (int k=0; k < nQuotient; k++) { if ( k == nQuotient-1) { strAnsHeader.SetAt(4, ' '); strAnsHeader.SetAt(5, ' '); } strTemp.Format(_T("08 %02d "), nCounter++); vctStrSaveCmd.push_back(strAnsHeader+strTemp+strAnsBody.Mid(0, 21)); strAnsBody=strAnsBody.Mid(21); }} return vctStrSaveCmd;

}

void COperationFile::deleteRepeatString(vector& vctStrSaveFileData) { // map

ifdef _UNICODE

int nLength = strSrc.GetLength();int nBytes = WideCharToMultiByte(CP_ACP,0,strSrc,nLength,NULL,0,NULL,NULL);char* VoicePath = new char[ nBytes + 1];memset(VoicePath,0,nLength + 1);WideCharToMultiByte(CP_ACP, 0, strSrc, nLength, VoicePath, nBytes, NULL, NULL); VoicePath[nBytes] = 0;strTarge = VoicePath;

else

strTarge = strSrc.GetBuffer(0);

endif

return strTarge;

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表