```c
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int GetValue(std::string strSource, std::string strkey)
{
if (strSource.empty())
{
return 0;
}
int npos = strSource.find(strkey);
int npos2 = strSource.find(" ", npos+ strkey.size());
string strx = strSource.substr(npos + strkey.size(), npos2 - npos - strkey.size());
return atoi(strx.c_str());
}
void main()
{
std::fstream readfile("2.txt");
char buf[512] = { 0 };
while (readfile.getline(buf, 512))
{
if (buf[0] == '\\' || buf[0] == '#'||buf[0] == '//')
{
continue;
}
cout << buf << endl;
std::string str = buf;
cout << GetValue(str, "x=")<<" " << GetValue(str, "y=") << " " << GetValue(str, "size=") << " " << GetValue(str, "type=") << endl;
}
readfile.close();
/*
#x:坐标 y:坐标 size:缺陷大小 type:缺陷类型(图标类型)
x=13 y=109 size=12 type=7
x=11 y=119 size=2 type=1
*/
system("pause");
}
结果:
但是推介下面这种方式读取文件
void main()
{
//#include <fstream>
std::fstream readfile("1.txt");
string strOut;
while (getline(readfile, strOut))
{
if (strOut[0] == '\\' || strOut[0] == '#' || strOut[0] == '//')
{
continue;
}
cout << strOut.c_str() << endl;
cout << GetValue(strOut, "x=") << " " << GetValue(strOut, "y=") << " " << GetValue(strOut, "size=") << " " << GetValue(strOut, "type=") << endl;
}
readfile.close();
system("pause");
}