Skip to content

Commit

Permalink
Write strings in CSV format in the examples in the docs.
Browse files Browse the repository at this point in the history
This makes the final flush of the file stream actually necessary.
  • Loading branch information
Lastique committed Jan 30, 2024
1 parent 7616f0b commit 989676e
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions doc/scope.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ public:
]]
[[
```
// Writes a list of strings to the file, one per line
bool save_to_file(std::vector< std::string > const& strings,
// Writes a list of strings to the file, in CSV format
bool save_as_csv(std::vector< std::string > const& strings,
std::string const& filename)
{
std::ofstream file(filename.c_str(),
Expand All @@ -249,14 +249,21 @@ bool save_to_file(std::vector< std::string > const& strings,
}
BOOST_SCOPE_EXIT_END;

bool first = true;
for (auto const& str : strings)
{
file << str << std::endl;
if (!first)
file << ',';
else
first = false;

file << '"' << str << '"';

if (file.fail())
return false;
}

file.flush();
file << std::endl;

// Commit the operation if the stream is not in a failed state
fail = file.fail();
Expand All @@ -267,8 +274,8 @@ bool save_to_file(std::vector< std::string > const& strings,
]
[
```
// Writes a list of strings to the file, one per line
bool save_to_file(std::vector< std::string > const& strings,
// Writes a list of strings to the file, in CSV format
bool save_as_csv(std::vector< std::string > const& strings,
std::string const& filename)
{
std::ofstream file(filename.c_str(),
Expand All @@ -284,14 +291,21 @@ bool save_to_file(std::vector< std::string > const& strings,
std::remove(filename.c_str());
});

bool first = true;
for (auto const& str : strings)
{
file << str << std::endl;
if (!first)
file << ',';
else
first = false;

file << '"' << str << '"';

if (file.fail())
return false;
}

file.flush();
file << std::endl;
if (file.fail())
return false;

Expand All @@ -304,8 +318,8 @@ bool save_to_file(std::vector< std::string > const& strings,
]
[
```
// Writes a list of strings to the file, one per line
bool save_to_file(std::vector< std::string > const& strings,
// Writes a list of strings to the file, in CSV format
bool save_as_csv(std::vector< std::string > const& strings,
std::string const& filename)
{
std::ofstream file(filename.c_str(),
Expand All @@ -321,14 +335,21 @@ bool save_to_file(std::vector< std::string > const& strings,
std::remove(filename.c_str());
}};

bool first = true;
for (auto const& str : strings)
{
file << str << std::endl;
if (!first)
file << ',';
else
first = false;

file << '"' << str << '"';

if (file.fail())
return false;
}

file.flush();
file << std::endl;
if (file.fail())
return false;

Expand Down

0 comments on commit 989676e

Please sign in to comment.