常量中有换行符(常量中有换行符怎么解决_字符型常量的表示方法)

发布时间:2025-12-10 20:02:57 浏览次数:2

常量中有换行符怎么解决_字符型常量的表示方法-qt常量中有换行符

常量中有换行符怎么解决_字符型常量的表示方法简介在使用MSVC编译的时候出现“常量中有换行符”这一错误,网上搜索后可知是由于文件是utf-8编码但没有带BOM导致的。解决方法有很多,因为感觉很麻烦,所以这里我使用python来给没有BOM的文件加上BOM。代码importoscur_dir=os.path.dirname(__file__)#deftest():#u8_nb_path=os.path.join(cur_dir,’utf-8_nobom.txt’)#u8_b_path=os.pat

简介

在使用MSVC编译的时候出现“常量中有换行符”这一错误,网上搜索后可知是由于文件是utf-8编码但没有带BOM导致的。解决方法有很多,因为感觉很麻烦,所以这里我使用python来给没有BOM的文件加上BOM。

代码

import oscur_dir = os.path.dirname(__file__)# def test():# u8_nb_path = os.path.join(cur_dir, 'utf-8_nobom.txt')# u8_b_path = os.path.join(cur_dir, 'utf-8_bom.txt')# u8_nb = open(u8_nb_path, "rb")# u8_b = open(u8_b_path, "rb")# print(u8_nb.read(1))# print(u8_nb.read(1))# print(u8_nb.read(1))# print(u8_nb.read(1))# print("")# print(u8_b.read(1))# print(u8_b.read(1))# print(u8_b.read(1))# print(u8_b.read(1))# # efbbbf# u8_nb.close()# u8_b.close()def has_bom(content):    return len(content) >= 3 and content[0] == 0xef \        and content[1] == 0xbb and content[2] == 0xbfdef to_utf8_bom_f(filename):    print("To utf8 with bom: " + filename)    f = open(filename, "rb")    content = f.read()    f.close()    if not has_bom(content):        f = open(filename, "wb")        # efbbbf        bom = bytes([0xef, 0xbb, 0xbf])        f.write(bom)        f.write(content)        f.close()def to_utf8_bom_d(dir, filters=["h", "cpp", "c"]):    for root, dirs, files in os.walk(dir):        for file in files:            splits = file.split('.')            if len(splits) >= 2 and splits[-1] in filters:                to_utf8_bom_f(os.path.join(root, file))def main():    src_dir = r"D:\Src\sqlitebrowser\git_repos\sqlitebrowser\src"    to_utf8_bom_d(src_dir)if __name__ == "__main__":    main()

是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活CLion DataGrip DataSpell dotCover dotMemory dotTrace GoLand PhpStorm PyCharm ReSharper ReShaC++ Rider RubyMine WebStorm 全家桶 刷新

【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…

使用方法

将代码中的src_dir修改成自己的代码目录即可(根据需要修改文件filter)。

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477