//******************************************************** // The following code example is taken from the book // C++23 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd23.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include // for std::cerr #include #include #include #include int main() { std::string filename = "tmp.txt"; std::ofstream file{filename}; if (!file) { std::println(std::cerr, "cannot open '{}'", filename); return 1; } // write the elements of a collection into the opened file: std::vector coll{"Rio", "Tokyo", "Kiev"}; for (const auto& elem : coll) { std::println(file, "City: {}", elem); // formatted output into a file } }