我要实现的功能是读下面文件。文本文件内容如下:
***************************************************************************
/*------------------
注解
---------------*///注解CTRLDATATYPE g_awViewClamp[] = {
DLG_PANEL, 10, 0*16 ,276, 11*16+4, CP_PANEL1, //DLG_STATIC, 23*8, 0*16 ,11, CS_STATIC1, VW_OVERVIEW_CLAMPPOSITION,
DLG_STATIC, 14*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_PRESS , //注解
DLG_STATIC, 20*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_SPEED ,
DLG_STATIC, 26*8, 0*16+1, 8, CS_STATIC2, VW_CLAMP_ENDPOSITION, //注解
DLG_END
};
......
......
......
**************************************************************************程序要求:
1.去除注解,去除空行,空格。
2.找到 “CTRLDATATYPE”,然后读 { }之間的内容。並提取逗號之間的内容。
***************************************************************************
/*------------------
注解
---------------*///注解CTRLDATATYPE g_awViewClamp[] = {
DLG_PANEL, 10, 0*16 ,276, 11*16+4, CP_PANEL1, //DLG_STATIC, 23*8, 0*16 ,11, CS_STATIC1, VW_OVERVIEW_CLAMPPOSITION,
DLG_STATIC, 14*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_PRESS , //注解
DLG_STATIC, 20*8, 0*16+1, 4, CS_STATIC2, VW_CLAMP_SPEED ,
DLG_STATIC, 26*8, 0*16+1, 8, CS_STATIC2, VW_CLAMP_ENDPOSITION, //注解
DLG_END
};
......
......
......
**************************************************************************程序要求:
1.去除注解,去除空行,空格。
2.找到 “CTRLDATATYPE”,然后读 { }之間的内容。並提取逗號之間的内容。
CStringArray strArray;
CString strRead, strTemp;
1,打开文件
2, while(file.ReadString(strRead) != ""){
if(strRead.Find("CTRLDATATYPE") != -1){
break;
}
}
3,strRead = "";
file.ReadString(strTemp);
while(strRead.File("},") == -1){
strRead += strTemp;
file.ReadString(strTemp);
}4, //解析strRead(数组内元素)
int nIndex = 0;
while((nIndex = strRead.Find(',') )!= -1){
strTemp = strRead.Left(nIndex);
strRead.Delete(0, nIndex+1);
strArray.Add(strTemp);}5, 提取strArray结果
4, //解析strRead(数组内元素)
int nIndex = 0;
while((nIndex = strRead.Find(',') )!= -1){
//去掉strRead开头的"//"
strTemp = strRead.Left(nIndex);
//去掉strTemp"//注解"和空格
strRead.Delete(0, nIndex+1);
strArray.Add(strTemp);}
{
const CString strChangeLine = "\r\n";//换行符,有的地方\n就可以换行
CFile file;
file.Open("a1.txt",CFile::modeRead|CFile::shareDenyNone); CString strCotent;
const int nLeng = file.GetLength() ;
file.Read(strCotent.GetBuffer(nLeng + 1),nLeng);
strCotent.ReleaseBuffer(); //除空格
strCotent.Remove(' ');
//除注释(假设没有递归注释)
//除去/* */
while(true)
{
int nPosBegin = strCotent.Find("/*");
int nPosEnd = strCotent.Find("*/");
if(-1 == nPosBegin || -1 == nPosEnd)
break;
strCotent.Delete(nPosBegin,nPosEnd - nPosBegin + 2);
} //除去行注释
while(true)
{
int nPosBegin = strCotent.Find("//");
int nPosEnd = strCotent.Find(strChangeLine,nPosBegin);
if(-1 == nPosBegin || -1 == nPosEnd)
break;
strCotent.Delete(nPosBegin,nPosEnd - nPosBegin + 2);
}
//如果有两个连续的换行符删除一个
int nPosEnd = 0 ;
while(true)
{
int nPosBegin = strCotent.Find(strChangeLine,nPosEnd);
nPosEnd = strCotent.Find(strChangeLine,nPosBegin+1);
if(-1 == nPosBegin || -1 == nPosEnd)
break;
if(nPosEnd - nPosBegin == strChangeLine.GetLength())
{
strCotent.Delete(nPosBegin,strChangeLine.GetLength());
nPosEnd -= strChangeLine.GetLength();
}
}
//如果以换行符开始删除
if(0 == strCotent.Find(strChangeLine))
strCotent = strCotent.Mid(strChangeLine.GetLength());
//把中间结果放进文件便于调试
CFile file2;
file2.Open("a2.txt",CFile::modeCreate|CFile::modeWrite);
file2.Write(strCotent,strCotent.GetLength());
file2.Close(); //取得{}的内容
int nPosBegin = strCotent.Find("{");
nPosEnd = strCotent.Find("}");
strCotent = strCotent.Mid(nPosBegin + 1 , nPosEnd - nPosBegin - 1); //用标号分隔的部分取出
CStringArray arStr;
while(true)
{
int nPos = strCotent.Find(",");
CString str ;
if(-1 == nPos)
{
str = strCotent;
arStr.Add(str);
break;
}
else
{
str = strCotent.Left(nPos );
strCotent = strCotent.Mid(nPos + 1);
arStr.Add(str);
}
}
file.Close();
}