1. 程式人生 > >如何用c++生成html5檔案並進行程式設計

如何用c++生成html5檔案並進行程式設計

有時候根據專案需要,在一個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();