2

fmt 库的 output_file 的诡异行为

 3 years ago
source link: https://zhiqiang.org/coding/output_file-of-fmt-is-weird.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

fmt 库的 output_file 的诡异行为

作者: 张志强

, 发表于 2020-11-03

, 共 443 字 , 共阅读 118 次

C++的fmt::format格式化库,由于效率高,功能强大,相关的 API 已经进入C++20,但其文档和 API 细节无法恭维。

比如下面这段代码来自 https://github.com/fmtlib/fmt,号称速度比fprintf要快 5 到 10 倍:

int main() {
  auto out = fmt::output_file("guide.txt");
  out.print("Don't {}", "Panic");
}

猜猜如果guide.txt里面有内容时会发生什么,删除文件重写还是续写?都不是,答案是从头开始覆盖写入。这意味着,如果文件原内容比要写的内容更长,文件就变成写入内容,再加上原内容剩下部分。

是不是很奇葩呀?关键是你在官方文档里还找不到output_file的任何信息( 2020 年 11 月 02 日)。在源代码里也没有该函数的帮助信息(只能看源实现代码)。

Q. E. D.

avatar-0.jpg

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK