我从我的程序中粘贴了一部分代码片段,代码的输出给出了一些不需要的字符,请查看它并让我知道其背后的明显逻辑原因。
class print {
FILE *pFile;
vector <char> st;
public:
char *formatForHtml(string str, string htmlTag) {
string strBegin;
strBegin = "<";
strBegin.append(htmlTag);
strBegin.append(">");
strBegin.append(str);
string strEnd = "</";
strEnd.append(htmlTag);
strEnd.append(">");
strBegin.append(strEnd);
st.resize(strBegin.size());
for (int i =0;i <strBegin.size();i++) {
st[i] = strBegin[i];
}
return &st[0];
}
void generatePath() {
char fileName[_MAX_FNAME];
_makepath(fileName,"C:","\\Users\\vinitg\\Documents\\Visual Studio 2010\\Projects\\continous_beam_cpp.cpp\\", "myf9", "html");
pFile = fopen(fileName,"a+");
}
void writeInFile() {
string str = "DESIGN OF A CONTINOUS BEAM!";
char *procString;
procString = formatForHtml(str, "p");
fprintf(pFile, procString);
fclose(pFile);
ShellExecute(NULL, "open", "file:///C:/Users/vinitg/Documents/Visual%20Studio%202010/Projects/continous_beam_cpp.cpp/myF9.html",NULL, NULL, SW_SHOWMAXIMIZED);
}
void passString (char g[]) {
cout << &g[0];
}
};
int main() {
print generateReport;
generateReport.generatePath();
generateReport.writeInFile();
cout << "Press any one key to continue..";
getch();
}`
您一定已经猜到创建的 HTML 文件并以 html 格式显示结果,下面粘贴的是输出(带有不需要的字符)
DESIGN OF A CONTINUOUS BEAM!
ýýýý««««««««îþ