From the course: C++ Best Practices for Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Writing strings to a file

Solution: Writing strings to a file - C++ Tutorial

From the course: C++ Best Practices for Developers

Start my 1-month free trial

Solution: Writing strings to a file

(light music) - [Instructor] Welcome back. Hopefully you solved the challenge. To solve this challenge, we need to use our knowledge of path and ofstream. We create the file using path, and write to it using ofstream. I also use a range-based for loop for the iteration. Let me show you my solution. But first, remember, there are lots of ways to write a program. Our solutions don't need to match. So, of course, we have our include files. I am creating a name space using boost::filesystem, and then I have my count to 10 function and the very first thing that I'm going to do in there is I'm creating a path variable and it's set to count.text. That is the name of the file that it's going to create. Then it's creating an output stream and it's going to be called ofs, and it's setting itself to what's contained inside of the path. And then I've got a range-based for loop here, and it's just got the numbers one through 10 in there. And then for each line, it's going to say line number of…

Contents