All right, let me help you with 12.5. So, I'm going to open up 12.5, and then click on there. And it says, "Write a definition of a function named F Copy." All right, so the function is going to be the return type, and then the function name, and then the parameters, and then we're going to go "curly brace, curly brace".
So the question is what is return type. "The function can be safely passed to fstream objects." So the parameters that we're going to pass, the function is going to receive two fstream objects. So it's going to be "fstream one", and "fstream two".
And then one is open for reading. We're going to read from this one. So I'm just call it "input", because we're input from the file. And then the other one's for writing. So this one right here I'm going to say "output". And then, "Assume that the input source is a text file consisting of a sequence of new line character delineated lines."
So means each piece of data is on a separate line. The function simply copies, line by line, the entire content of the data source associated with the first argument to the data target associated with the second argument, and then no value is returned. So for the method, no value is returned, so therefore the return type is going be void.
All right, so we need to read from the first file, and copy it into the second file. So how do we read from the file? Well let's think about this. If I were reading from the console, how would I read from the console? I would do a get line, and then I would read from the console, and then I would put it into some variable.
So let's create some variable. So, if I say, "string line", initialize it to quote quote. Just have a default initialization, right. Then I would read from the console, and I would put it into that line like that. Now here's the thing about it. Reading and writing to the file is very much the same as reading and writing to the console. So instead of using the console, I would simply use my input stream. That's it. All right, so reading and writing from the file is very much like reading and writing for the console.
All right, so now they want us to then output to the file the line from this file. So if I want to, the line that I just read from the file, if I want to put it to the console, what would I do? I would just say "cout", and then I would say "line", and then I would say "endl". Just like that. And this would put to the console whatever was read into that line, but I don't want to do it to the console. I actually want it to my output file.
So here, instead of using "cout", I'm going to use my output file stream. And I'm done. That's it. It's literally that easy. Okay? Now I've got a little bug in here, and when I run this, or when I submit this, you'll see the bug that I've got. But otherwise this right here is perfect.
So I'm going to hit Submit, and it says, "Wait a minute, wait a minute. The identifiers were all unexpected," and then they give you hints, and then they say, "You most certainly should be using an ampersand," and, "You most certainly should be using a 'while'." So the "while" means we should be reading every single line, and you need to keep reading while there are more lines.
So I'm going to come back over here. So the first thing I'm going to do is I'm going to pop up here, and my streams, I need to use an ampersand because if you don't use an ampersand then it will make a copy of the stream, and we don't want that. We want to use the actual stream that was created. So you need to use an ampersand to get the address of the stream so that you receive it by reference. All right, so the ampersand up here is important.
Now the other thing is this right here only reads the first line, and that's it. But we want to read every single line in the file. So I'm going to say "while", and then I'm going to say my input file stream, I'm going to use the "peek" command on it, and while that peek command gives me back a negative one, something that's bigger than a negative one, then I'm going to read each line and copy each line out to the other file. So it'll look like this. And then let's see if that tabs. Yes, that tabs all right.
So while we have data, I'm going to read the line, and then I'm going to output it to the other file. Just like that. So the way the "peek" command works is it looks at the next character without using it up, it just looks at it, and it gives you back the ASCII value for that next character. If the ASCII value is a whole number, then that means we have data. If it peeks and there's no more data, it gives you back a negative one. And negative one is universal for error in the software development community. All right, so if you get back a negative one, you know something went wrong.
All right, so with this right here, it gives back, as long as we don't get a negative one, have data. So as long as we get something greater than negative one, we have data. So we're going to read the line, and then we're going to output it to our other file. And that is 12.5.
[End of Audio]