Skip to content

Commit f709a3d

Browse files
authored
feat(file_system): Update example with explicit tests of append and truncate file stream open flags (#542)
* feat(file_system): Update example with explicit tests of append and truncate file stream open flags * minor cleanup
1 parent 2cb0da6 commit f709a3d

File tree

1 file changed

+66
-16
lines changed

1 file changed

+66
-16
lines changed

components/file_system/example/main/file_system_example.cpp

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,13 @@ extern "C" void app_main(void) {
214214
fs::path file = sandbox / fs::path{test_file};
215215

216216
// write to a file
217-
std::ofstream ofs(file);
218-
ofs << file_contents;
219-
ofs.close();
220-
ofs.flush();
221-
logger.info("Wrote '{}' to {}", file_contents, file.string());
217+
{
218+
std::ofstream ofs(file);
219+
ofs << file_contents;
220+
ofs.close();
221+
ofs.flush();
222+
logger.info("Wrote '{}' to {}", file_contents, file.string());
223+
}
222224

223225
// Get file size
224226
size_t file_size = fs::file_size(file, ec);
@@ -230,17 +232,65 @@ extern "C" void app_main(void) {
230232
}
231233

232234
// read from a file
233-
std::ifstream ifs(file, std::ios::in | std::ios::binary | std::ios::ate); // at the end
234-
// ifstream::pos_type file_size = ifs.tellg(); // an alternate way to get size
235-
ifs.seekg(0, std::ios::beg);
236-
// read bytes
237-
std::vector<char> file_bytes(file_size);
238-
ifs.read(file_bytes.data(), file_size);
239-
// convert bytes to string_view
240-
std::string_view file_string(file_bytes.data(), file_size);
241-
logger.info("Read bytes from file: {}", file_bytes);
242-
logger.info("Read string from file: {}", file_string);
243-
ifs.close();
235+
{
236+
std::ifstream ifs(file, std::ios::in | std::ios::binary);
237+
// you can also get size if you use std::ios::ate flag:
238+
// ifstream::pos_type file_size = ifs.tellg();
239+
ifs.seekg(0, std::ios::beg);
240+
// read bytes
241+
file_size = fs::file_size(file, ec);
242+
std::vector<char> file_bytes(file_size);
243+
ifs.read(file_bytes.data(), file_size);
244+
// convert bytes to string_view
245+
std::string_view file_string(file_bytes.data(), file_size);
246+
logger.info("Read bytes from file: {}", file_bytes);
247+
logger.info("Read string from file: {}", file_string);
248+
ifs.close();
249+
}
250+
251+
// append to the file
252+
{
253+
std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::app);
254+
ofs << " - Appended!";
255+
logger.info("Appended file");
256+
ofs.close();
257+
}
258+
259+
// read from an appended file
260+
{
261+
std::ifstream ifs(file, std::ios::in | std::ios::binary);
262+
// read bytes
263+
file_size = fs::file_size(file, ec);
264+
std::vector<char> file_bytes(file_size);
265+
ifs.read(file_bytes.data(), file_size);
266+
// convert bytes to string_view
267+
std::string_view file_string(file_bytes.data(), file_size);
268+
logger.info("Read bytes from appended file: {}", file_bytes);
269+
logger.info("Read string from appended file: {}", file_string);
270+
ifs.close();
271+
}
272+
273+
// overwrite (truncate) the file
274+
{
275+
std::ofstream ofs(file, std::ios::out | std::ios::binary | std::ios::trunc);
276+
ofs << "Truncated!";
277+
logger.info("Overwrote file");
278+
ofs.close();
279+
}
280+
281+
// read from a file (again)
282+
{
283+
std::ifstream ifs(file, std::ios::in | std::ios::binary);
284+
// read bytes
285+
file_size = fs::file_size(file, ec);
286+
std::vector<char> file_bytes(file_size);
287+
ifs.read(file_bytes.data(), file_size);
288+
// convert bytes to string_view
289+
std::string_view file_string(file_bytes.data(), file_size);
290+
logger.info("Read bytes from truncated / overwritten file: {}", file_bytes);
291+
logger.info("Read string from truncated / overwritten file: {}", file_string);
292+
ifs.close();
293+
}
244294

245295
// rename the file
246296
fs::path file2 = sandbox / "test2.csv";

0 commit comments

Comments
 (0)