有时候根据项目需要,在一个c++项目里面,需要对某些结果保存为.htm或者.html文件,并根据需要,在visual studio2013/2015的c++环境下对该.html文件进行编程。这样做,一方面为了输出结果需要,一方面便于浏览器打开。
首先
建立3个.html文件,并设置好属性:
ofstream totalHtml,passHtml,failHtml;
totalHtml.open(imgsSavePath + "/total.html", ios::out | ios::trunc);// 文件若不存在,就新建;若存在,则覆盖
passHtml.open(imgsSavePath + "/pass.html", ios::out | ios::trunc);
failHtml.open(imgsSavePath + "/fail.html", ios::out | ios::trunc);
然后
往3个.html文件里面写入html程序,这里要注意:
1、所有的html程序全部在“”里面;
2、如果html程序里面需要用到“,此时注意转义字符“\”的使用。
//pass.html和fail.html文件头处理及相关类的样式定义
passHtml << "<head>";
passHtml << "<meta http-equiv=\"Content-type\" content=\"text/css;charset=utf-8\"/>";
passHtml << "<title>Pass-Components</title>";
passHtml << "<style type=\"text/css\" media=\"screen\">";
passHtml << "body{font-family:Arial;font-size:small;text-align:Center;color:Black;background:White;}";
passHtml << "table{margin-left:auto;margin-right:auto;text-align:center;}";
passHtml << ".step_row_pass{text-align:center;background-color:rgb(0,255,0);vertical-align:middle;border:Black;padding:1%;}";
passHtml << "</style></head>";
passHtml << "<h3 style = \"background-color:rgb(0,255,0);text-align:center\">Passed Components List</h3>";
passHtml << "<table width=\"50%\"><tbody>";
failHtml << "<head>";
failHtml << "<meta http-equiv=\"Content-type\" content=\"text/css;charset=utf-8\"/>";
failHtml << "<title>Fail-Components</title>";
failHtml << "<style type=\"text/css\" media=\"screen\">";
failHtml << "body{font-family:Arial;font-size:small;text-align:Center;color:Black;background:White;}";
failHtml << "table{margin-left:auto;margin-right:auto;text-align:center;}";
failHtml << ".step_row_fail{text-align:center;color:black;background-color:rgb(255,128,128);vertical-align:middle;border:Silver;padding:1%;}";
failHtml << "</style></head>";
failHtml << "<h3 style =\"background-color:rgb(255,128,128);text-align:center\">Fault Components List</h3>";
failHtml << "<table width=\"50%\"><tbody>";
再然后
就是对3个.html文件的“table”进行一行行增加,这里我用的是while循环:
while (mImagesListRead.good() && !mImagesListRead.eof()) //循环读取图像序列
{
...
failHtml << "<tr><td width=\"10%\" class=\"step_row_fail\">" << mImageId << "</td>";
failHtml<<"<td width=\"10%\" class=\"step_row_fail\">fault</td>";
failHtml << "<td class=\"step_row_fail\"><img src=\"./FaultImgs/"<< mImageId << ".png\"/> </td></tr>";
faultnum++;
...
}
最后注意收尾
totalHtml << "<title>Result</title>";
totalHtml << "<h3 style=\"background-color:gray;color:blue;text-align:center\">Analysis Results</h3><hr/>";
totalHtml << "<p width=\"50%\" style=\"background-color:yellow;text-align:center\">Components Number: " << mImagesCount << "</p><hr/>";
totalHtml << "<base target=\"_blank\" /> <p style = \"background-color:rgb(0,255,0);text-align:center\">";
totalHtml<<"<a href =\"pass.html\"><b>Pass</b>-Components</a>: " << mImagesCount - faultnum << "</p><hr/>";
totalHtml << "<p style =\"background-color:rgb(255,128,128);text-align:center\">";
totalHtml<<"<a href =\"fail.html\"><b>Fail</b>-Components</a>: " << faultnum << "</p>";
totalHtml.close();
passHtml << "</tbody></table>";
passHtml.close();
failHtml << "</tbody></table>";
failHtml.close();