Truncate file by default

This commit is contained in:
Victor Zverovich 2020-11-27 08:15:14 -08:00
parent 22a68d1613
commit 119f7dc3d6
2 changed files with 16 additions and 2 deletions

View File

@ -280,7 +280,8 @@ class file {
WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only. WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing. RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing.
CREATE = FMT_POSIX(O_CREAT), // Create if the file doesn't exist. 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. // Constructs a file object which doesn't represent any file.
@ -357,7 +358,7 @@ struct buffer_size {
}; };
struct ostream_params { 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; size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
ostream_params() {} ostream_params() {}

View File

@ -319,6 +319,19 @@ TEST(OStreamTest, BufferSize) {
EXPECT_READ(in, "foo"); 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) { TEST(FileTest, DefaultCtor) {
file f; file f;
EXPECT_EQ(-1, f.descriptor()); EXPECT_EQ(-1, f.descriptor());