2011-11-17 106 views

回答

3

只要把BOM成字符串和寫入到文件(使用你用什麼方法,例如流或經典cstdio):

const char *bom = "\xef\xbb\xbf"; // UTF-8 
const char *bom_alt = {0xef, 0xbb, 0xbf, 0}; // the null termination is optional, depending on how you write it later on 

然後你只需要確保你」實際上編寫正確的UTF-8並且沒有ANSI或寬字符串。我建議使用UTF8-CPP(在工作項目中已經使用過它,並且完美地工作,而不必使用一些巨大的庫,如Boost)。

1

如果您使用Qt庫,則可以使用QTextStream::setGenerateByteOrderMark(bool)來添加BOM。