我在打印一個字符串時出於調試目的使用了一個問題。用fprintf打印一個字符串
我創建的字符串,像這樣:
//checker is int
std::stringstream buttonx;
buttonx << "Button" << checker << "_x";
現在,我嘗試將它打印到我error.txt
文件
FILE * p;
p = fopen ("error.txt","w");
fprintf(p, "%s" , buttonx.str());
fclose(p);
輸出是:
,æ0
它每次都不一樣。我不確定發生了什麼事情,希望有人能夠解釋這個錯誤?
或者,使用'std :: ofstream'作爲C++神的意圖。 – cHao
std :: ofstream語法怎麼看? – Sir
@Dave:'std :: ofstream f(「error.txt」); f << buttonx.str();'。或者,如果你沒有給文件寫任何東西,甚至可以使用'std :: ofstream(「error.txt」)<< buttonx.str();'。 – cHao