用于解决各个版本VS编译提示:
warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失的问题
利用Python实现文件判断编码然后转换到指定编码以解决此提示。
import chardet
import shutil
import os
def CHECK_UTF8SIG(rpath):
f = open(rpath,'rb')
data = f.read()
rt=chardet.detect(data)
if(rt['encoding'] == "UTF-8-SIG"):
return 1,rt['encoding'];
return 0,rt['encoding'];
def FilesCodingCorrection(rpath):
rpathcopy = rpath+".tcopytemp"
r,charset=CHECK_UTF8SIG(rpath)
if(r==0):
print(charset,rpath)
shutil.copy(rpath,rpathcopy);
with open(rpath, 'w', encoding='utf-8-sig') as fp:
for row in open(rpathcopy,'r', encoding=charset):
fp.write(row)
os.remove(rpathcopy)
fp.close()
def GetDirsFilesList(ROOTDIR,LIST_PATH):
list = os.listdir(ROOTDIR)
for i in range(0, len(list)):
path = os.path.join(ROOTDIR, list[i])
if os.path.isfile(path):
ext=os.path.splitext(path)[1]
if((".cpp" == ext)|(".hpp" == ext)|(".h" == ext)|(".c" == ext)):
#print (path)
LIST_PATH.append(path)
else:
GetDirsFilesList(path,LIST_PATH)
if __name__ == '__main__':
CorrectionSet=[];
#"..\\YourDirs\\" 需要转换的总目录
GetDirsFilesList("..\\YourDirs\\",CorrectionSet)
print(CorrectionSet)
for rcpath in CorrectionSet:
FilesCodingCorrection(rcpath)
将此Python代码文件保存为recoding.py放在VS项目目录中,然后再项目生产前事件内加入如下命令。
编译后即可转换文件编码以消除编码提示。