In this video, we'll write some code that will allow us to create some files,
and we'll create some files a couple of different ways.
First, we'll create a real simple file just write a string to it.
And then what we'll do is we'll write a couple of copy file utilities, just like we did in the slides.
Okay. So I'm in the section 19 workspace in the write file project.
And I've got nothing going on here except these includes. I'm including iostream,
fstream and string.
Let's get started. First thing we need to do is let's create an
ofstream object. So we'll call it std ofstream,
and we'll call it out file since it's going to be an output file,
and we'll initialize it to dot dot slash, again, I'm in CodeLite that's why I'm
doing this, and I'll name the file output.txt.
Okay. Now we'll check to see that it was opened correctly.
So if not out file,
then what we're going to say is just get out of here, right, some error.
So we'll provide an error, saying error creating file
because it is an output file.
And we'll return something other than 0.
Let me get rid of that extra quote right here, and we'll return one.
That's it so if we get now to this line 13, we know our file has been
created in this case. Remember, if the file does not exist,
it's going to be created. Right now it does not exist, so it will be created.
If it does exist, it'll be truncated. Okay. So we'll lose whatever was in there before.
So let's create a string variable.
We'll just call it line,
and we'll ask the user we can call that variable name anything we want.
We'll ask the user to enter something
to write to the file.
And we'll simply read it from --
we'll use getline to read it because we want to read more than just one word. So we'll say getline
from cin into line.
Remember, again,
we're using string objects here, we're not using c-style strings.
That's it. So what do we do now. We say out file,
and we insert that line into that file,
and we'll give it a new line as well. Remember, endline will flush out the buffer,
and then we're done here we'll just close the file.
That's it, pretty easy. Now if we run this,
let's do that.
Enter something to write to the file, I'm going to say hello
there file, I'll press enter, program is done.
I'll exit this. Now there is nothing that shows up here by default.
Some of the IDEs will show that file when it's created,
CodeLite doesn't. So what you can do is you can select the project
right click and say open containing folder,
that'll open it up in explorer or in finder if you're on a mac.
And I'll just drag that over here.
And you can see that's right here we just created the file name. Remember -- let me drag this down just a little bit.
The file name was output.txt. There's output.txt right here.
And you can see the contents of it right over here hello there file.
Okay. Now let me move this out of the way, and let's run this again.
And if we run it again,
I'll just say my name is Frank
and press enter.
And now if we look at the file again, I'll drag this back over.
You can see that what was there before was erased
because we're opening the file in truncation mode so it gets rid of whatever was there.
That's the default. Now if we want to add something to the file,
we can simply open it in the pen mode. So we can put a another flag here and just say --
we'll say std ios append mode or app.
And if we run this again now, we say this
should be another line.
Okay. So we'll take a look at the file now.
And you can see my name is Frank, and now I put this should be another line.
Okay. So you can see that it's actually appending to the existing file.
So that's it. That's pretty straightforward and really that's the basics
of output to text files.
We have the power of all those stream manipulators,
which really gives us a lot of power into how we format the output files.
Okay. So what I want to do now is I want to switch to
another project.
It's going to be the CopyFile_1 project. So I'll switch to that, and I'll be right back.
Okay. So I'm in the section 19 workspace.
And I've opened up now the CopyFile_1 project.
And in this project, what we've got is this poem. We've got
Shakespeare's sonnet again. And what we want to do is we just want to copy it to another file.
So what we can do is I've already written the code, and it's really really straightforward.
It's just like it was in the slides. What we can do is we can start here.
And you can see we've got our includes that we need,
and we've got an input file stream called infile, which is my poem.
And then I've got an output file stream called out file,
which is my poem_out.txt that's the file we want to create
that's going to be a copy of the poem.txt file.
So again, we're basically following the recipe. We're instantiating those objects.We're making sure that
our file streams are open and ready to read and write.
And then we've got our logic. And our logic is really only just a couple of lines of code.
We've got a line that we want to read file -- the file input into, and there's my loop.
I'm using getline to read a line from infile.
And when I get that line, all I'm going to do is write it to out file. That's it.
At the end, we're going to display a little message that says file copied.
And we're going to close both files. So if we run this,
it'll say file copied. That's it. You can see how quickly this runs.
And we'll open up that project, the containing folder. And we should see
a poem underscore out.
There it is right here that was just created 1k big.
And if I select it, you can see that the poem is over here.
This is the original poem, and this is the copied poem. So it's exactly the same.
So it's really easy to copy one file to another.
In this case, we used formatted input and output because we're using getline,
and we're using the insertion operator with line to write it to the file.
That's it. Now if we go over to CopyFile_2 project,
let me open that up.
It's exactly the same thing. The only difference here is that we're using get and put. . So we're using
unformatted input output.
And you can see that the code is exactly the same except the while loop and the
variable declaration right there. That's the only difference is we're doing it one character at a time.
So while we get a character from infile,
we output that character to outfile. That's it.
So let's run this. I'll select that, make sure that that project is selected.
I'll run this. It's done.
And I'll open up that project in the containing folder.
And right here, you'll see poem.out.
And it's right over here, you can see it right there.
So that's it for this video. Here you can see a few examples of creating files,
appending to files and copying files using unformatted and formatted io.
Next is a challenge. And in the challenge what we're going to do is we're going to take
Shakespeare's, Romeo and Juliet and add line numbers to it and create a new file.
I'll see you there.