Skip to content

Commit

Permalink
Truncate file by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Nov 27, 2020
1 parent 22a68d1 commit 119f7dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/fmt/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ class file {
WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing.
CREATE = FMT_POSIX(O_CREAT), // Create if the file doesn't exist.
APPEND = FMT_POSIX(O_APPEND) // Open in append mode.
APPEND = FMT_POSIX(O_APPEND), // Open in append mode.
TRUNC = FMT_POSIX(O_TRUNC) // Truncate the content of the file.
};

// Constructs a file object which doesn't represent any file.
Expand Down Expand Up @@ -357,7 +358,7 @@ struct buffer_size {
};

struct ostream_params {
int oflag = file::WRONLY | file::CREATE;
int oflag = file::WRONLY | file::CREATE | file::TRUNC;
size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;

ostream_params() {}
Expand Down
13 changes: 13 additions & 0 deletions test/os-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ TEST(OStreamTest, BufferSize) {
EXPECT_READ(in, "foo");
}

TEST(OStreamTest, Truncate) {
{
fmt::ostream out = fmt::output_file("test-file");
out.print("0123456789");
}
{
fmt::ostream out = fmt::output_file("test-file");
out.print("foo");
}
file in("test-file", file::RDONLY);
EXPECT_EQ("foo", read(in, 4));
}

TEST(FileTest, DefaultCtor) {
file f;
EXPECT_EQ(-1, f.descriptor());
Expand Down

0 comments on commit 119f7dc

Please sign in to comment.