From f87e18702647f12dd3e5b40cc9d63bb59d61889c Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 22 Oct 2025 09:09:32 -0500 Subject: [PATCH 1/2] feat(file_system): Update example with explicit tests of append and truncate file stream open flags --- .../example/main/file_system_example.cpp | 87 +++++++++++++++---- 1 file changed, 71 insertions(+), 16 deletions(-) diff --git a/components/file_system/example/main/file_system_example.cpp b/components/file_system/example/main/file_system_example.cpp index 9f72c8e5c..4c454400e 100644 --- a/components/file_system/example/main/file_system_example.cpp +++ b/components/file_system/example/main/file_system_example.cpp @@ -214,11 +214,13 @@ extern "C" void app_main(void) { fs::path file = sandbox / fs::path{test_file}; // write to a file - std::ofstream ofs(file); - ofs << file_contents; - ofs.close(); - ofs.flush(); - logger.info("Wrote '{}' to {}", file_contents, file.string()); + { + std::ofstream ofs(file); + ofs << file_contents; + ofs.close(); + ofs.flush(); + logger.info("Wrote '{}' to {}", file_contents, file.string()); + } // Get file size size_t file_size = fs::file_size(file, ec); @@ -230,17 +232,70 @@ extern "C" void app_main(void) { } // read from a file - std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end - // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size - ifs.seekg(0, std::ios::beg); - // read bytes - std::vector file_bytes(file_size); - ifs.read(file_bytes.data(), file_size); - // convert bytes to string_view - std::string_view file_string(file_bytes.data(), file_size); - logger.info("Read bytes from file: {}", file_bytes); - logger.info("Read string from file: {}", file_string); - ifs.close(); + { + std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end + // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size + ifs.seekg(0, std::ios::beg); + // read bytes + file_size = fs::file_size(file, ec); + std::vector file_bytes(file_size); + ifs.read(file_bytes.data(), file_size); + // convert bytes to string_view + std::string_view file_string(file_bytes.data(), file_size); + logger.info("Read bytes from file: {}", file_bytes); + logger.info("Read string from file: {}", file_string); + ifs.close(); + } + + // append to the file + { + std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::app | + std::ios::ate); // at the end + ofs << " - Appended!"; + logger.info("Appended file"); + ofs.close(); + } + + // read from an appended file + { + std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end + // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size + ifs.seekg(0, std::ios::beg); + // read bytes + file_size = fs::file_size(file, ec); + std::vector file_bytes(file_size); + ifs.read(file_bytes.data(), file_size); + // convert bytes to string_view + std::string_view file_string(file_bytes.data(), file_size); + logger.info("Read bytes from appended file: {}", file_bytes); + logger.info("Read string from appended file: {}", file_string); + ifs.close(); + } + + // overwrite (truncate) the file + { + std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::trunc | + std::ios::ate); // at the end + ofs << "Truncated!"; + logger.info("Overwrote file"); + ofs.close(); + } + + // read from a file (again) + { + std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end + // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size + ifs.seekg(0, std::ios::beg); + // read bytes + file_size = fs::file_size(file, ec); + std::vector file_bytes(file_size); + ifs.read(file_bytes.data(), file_size); + // convert bytes to string_view + std::string_view file_string(file_bytes.data(), file_size); + logger.info("Read bytes from truncated / overwritten file: {}", file_bytes); + logger.info("Read string from truncated / overwritten file: {}", file_string); + ifs.close(); + } // rename the file fs::path file2 = sandbox / "test2.csv"; From 27897e530a74815fa9773a38a0f5f657a9bfb851 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 22 Oct 2025 09:16:06 -0500 Subject: [PATCH 2/2] minor cleanup --- .../example/main/file_system_example.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/components/file_system/example/main/file_system_example.cpp b/components/file_system/example/main/file_system_example.cpp index 4c454400e..747c0a169 100644 --- a/components/file_system/example/main/file_system_example.cpp +++ b/components/file_system/example/main/file_system_example.cpp @@ -233,8 +233,9 @@ extern "C" void app_main(void) { // read from a file { - std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end - // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size + std::ifstream ifs(file, std::ios::in | std::ios::binary); + // you can also get size if you use std::ios::ate flag: + // ifstream::pos_type file_size = ifs.tellg(); ifs.seekg(0, std::ios::beg); // read bytes file_size = fs::file_size(file, ec); @@ -249,8 +250,7 @@ extern "C" void app_main(void) { // append to the file { - std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::app | - std::ios::ate); // at the end + std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::app); ofs << " - Appended!"; logger.info("Appended file"); ofs.close(); @@ -258,9 +258,7 @@ extern "C" void app_main(void) { // read from an appended file { - std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end - // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size - ifs.seekg(0, std::ios::beg); + std::ifstream ifs(file, std::ios::in | std::ios::binary); // read bytes file_size = fs::file_size(file, ec); std::vector file_bytes(file_size); @@ -274,8 +272,7 @@ extern "C" void app_main(void) { // overwrite (truncate) the file { - std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::trunc | - std::ios::ate); // at the end + std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::trunc); ofs << "Truncated!"; logger.info("Overwrote file"); ofs.close(); @@ -283,9 +280,7 @@ extern "C" void app_main(void) { // read from a file (again) { - std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end - // ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size - ifs.seekg(0, std::ios::beg); + std::ifstream ifs(file, std::ios::in | std::ios::binary); // read bytes file_size = fs::file_size(file, ec); std::vector file_bytes(file_size);