MFC中for循环查找字符串并输出问题,

h1.txt

*#2%

9c28ef527888a8

2405F5f4da2aac

@

h2.txt

E043DB (base 16) Shenzhen ViewAt Technology Co.,Ltd.

2405F5 (base 16) Integrated Device Technology (Malaysia)

要求查找h1.txt每行前6位是否存在于h2.txt,

代码

CStdioFile file;
int i;
file.Open("F:\h1.txt",CFile::modeRead);//h1.txt是串口接收到的MAC地址
CString str1,strd;
for(i=0;file.ReadString(str1);i++)
{
if(str1.Find('*')>=0)
{
strd+="数据包开始,包含"+str1.Mid(2,str1.Find('%')-2)+"个MAC地址:";
strd+="\r\n";
}
else if(str1.Find('@')>=0)
{
strd+="数据包结束!";
strd+="\r\n";
}
else
{
CStdioFile read;
int j;
read.Open("F:\h2.txt",CFile::modeRead);//h2.txt是MAC与厂商标准库
CString str11,strx;
for(j=0;read.ReadString(str11);j++)
{
CString strTemp;
CString buf1[2048],buf2[2048],buf3[2048];
buf1[i]=str1.Mid(0,6);//读取g2中每行0-6位之前的内容到buf1
buf2[j]=str11.Left(str11.Find(' '));//读取g1中每行空格符之前的内容到buf2
buf3[j]=str11.Mid(str11.Find('\t')+2).Trim();

            if(buf2[j].Find(buf1[i])>=0)
            {
                strTemp+="MAC地址:"+str1.Mid(0,12)+"\t厂商:"+buf3[j];
                strTemp+="\r\n";
            }
            else //if(buf1[i].Find(buf2[j]==-1))
            {
                strTemp+="MAC地址:"+str1.Mid(0,12)+"\t厂商:NULL";
                strTemp+="\r\n";
            }

            strx+=strTemp;
        }

        strd+=strx;

    }
}
SetDlgItemText(IDC_EDIT1,strd);

查找结果如下

数据包开始,包含2个MAC地址:

MAC地址:9c28ef527888 厂商:NULL

MAC地址:9c28ef527888 厂商:NULL

MAC地址:2405F5f4da2a 厂商:NULL

MAC地址:2405F5f4da2a 厂商:Integrated Device Technology (Malaysia)

数据包结束!

怎么能使输出为

数据包开始,包含2个MAC地址:

MAC地址:9c28ef527888 厂商:NULL

MAC地址:2405F5f4da2a 厂商:Integrated Device Technology (Malaysia)

数据包结束!

使用map ,用mac地址作为key,其他作为value,封装一个结构,每次存的时候,会自动判断是否有重复的mac

用set就可以

 #include <iostream> 
#include <set> 
#include <fstream> 
#include <string> 
using namespace std; 
int main() 
{ 
set <string> s; 
string str; 
ifstream in("输入.txt"); 
ofstream out("输出.txt"); 
while(in.getlin(str),!in.eof()) 
{ 
s.insert(str); 
} 
for(set <string>::iterator it=s.begin();it!=s.end();++s) 
out < <*it < <"\n"; 
in.close(); 
out.close(); 
}