Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt::output_file doesn't erase contents of existing file #2018

Closed
bluebear94 opened this issue Nov 14, 2020 · 2 comments
Closed

fmt::output_file doesn't erase contents of existing file #2018

bluebear94 opened this issue Nov 14, 2020 · 2 comments

Comments

@bluebear94
Copy link

I'm not sure what the intended behavior is, but fmt::output_file, when opened on an existing file, does not clear its contents. That is, given this code:

auto out = fmt::output_file("data/test.txt");
out.print("Hello World!\n");

and this text in data/test.txt:

This should be overwritten completely!

the file looks like this after running the code:

Hello World!
e overwritten completely!

I believe this behavior is not intuitive and can cause subtle bugs; therefore, the default should be changed to clear all the contents, or at least it should be documented.

  • fmt version: 7.1.2
  • OS: Linux
@vitaut
Copy link
Contributor

vitaut commented Nov 14, 2020

You can pass the usual open flags to file_open, e.g.

 auto out = fmt::output_file("test.txt", O_WRONLY | O_TRUNC);

A PR to switch the default to truncate is welcome.

@vitaut vitaut closed this as completed Nov 14, 2020
@vitaut
Copy link
Contributor

vitaut commented Nov 27, 2020

Switched to truncation by default in 119f7dc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants