1 00:00:05,500 --> 00:00:08,900 In this video, we'll write some code that will allow us to create some files, 2 00:00:08,900 --> 00:00:11,100 and we'll create some files a couple of different ways. 3 00:00:11,100 --> 00:00:14,300 First, we'll create a real simple file just write a string to it. 4 00:00:14,300 --> 00:00:19,100 And then what we'll do is we'll write a couple of copy file utilities, just like we did in the slides. 5 00:00:20,660 --> 00:00:25,360 Okay. So I'm in the section 19 workspace in the write file project. 6 00:00:25,720 --> 00:00:29,420 And I've got nothing going on here except these includes. I'm including iostream, 7 00:00:29,420 --> 00:00:30,920 fstream and string. 8 00:00:31,470 --> 00:00:34,170 Let's get started. First thing we need to do is let's create an 9 00:00:34,170 --> 00:00:37,370 ofstream object. So we'll call it std ofstream, 10 00:00:38,520 --> 00:00:42,720 and we'll call it out file since it's going to be an output file, 11 00:00:42,720 --> 00:00:46,520 and we'll initialize it to dot dot slash, again, I'm in CodeLite that's why I'm 12 00:00:46,520 --> 00:00:50,520 doing this, and I'll name the file output.txt. 13 00:00:53,520 --> 00:00:57,180 Okay. Now we'll check to see that it was opened correctly. 14 00:00:57,180 --> 00:00:58,680 So if not out file, 15 00:00:59,780 --> 00:01:03,140 then what we're going to say is just get out of here, right, some error. 16 00:01:04,129 --> 00:01:08,130 So we'll provide an error, saying error creating file 17 00:01:09,900 --> 00:01:11,400 because it is an output file. 18 00:01:13,760 --> 00:01:16,010 And we'll return something other than 0. 19 00:01:17,780 --> 00:01:21,280 Let me get rid of that extra quote right here, and we'll return one. 20 00:01:22,980 --> 00:01:27,230 That's it so if we get now to this line 13, we know our file has been 21 00:01:27,230 --> 00:01:30,590 created in this case. Remember, if the file does not exist, 22 00:01:30,590 --> 00:01:34,390 it's going to be created. Right now it does not exist, so it will be created. 23 00:01:34,590 --> 00:01:38,740 If it does exist, it'll be truncated. Okay. So we'll lose whatever was in there before. 24 00:01:38,740 --> 00:01:40,400 So let's create a string variable. 25 00:01:41,060 --> 00:01:43,420 We'll just call it line, 26 00:01:44,300 --> 00:01:48,180 and we'll ask the user we can call that variable name anything we want. 27 00:01:48,180 --> 00:01:50,680 We'll ask the user to enter something 28 00:01:51,340 --> 00:01:52,940 to write to the file. 29 00:01:55,600 --> 00:01:57,600 And we'll simply read it from -- 30 00:01:59,100 --> 00:02:03,090 we'll use getline to read it because we want to read more than just one word. So we'll say getline 31 00:02:03,590 --> 00:02:05,790 from cin into line. 32 00:02:07,790 --> 00:02:08,789 Remember, again, 33 00:02:09,690 --> 00:02:13,890 we're using string objects here, we're not using c-style strings. 34 00:02:14,890 --> 00:02:17,590 That's it. So what do we do now. We say out file, 35 00:02:19,590 --> 00:02:23,390 and we insert that line into that file, 36 00:02:23,390 --> 00:02:27,380 and we'll give it a new line as well. Remember, endline will flush out the buffer, 37 00:02:27,740 --> 00:02:30,240 and then we're done here we'll just close the file. 38 00:02:32,600 --> 00:02:35,200 That's it, pretty easy. Now if we run this, 39 00:02:35,860 --> 00:02:36,850 let's do that. 40 00:02:39,350 --> 00:02:41,710 Enter something to write to the file, I'm going to say hello 41 00:02:43,210 --> 00:02:47,210 there file, I'll press enter, program is done. 42 00:02:47,210 --> 00:02:51,200 I'll exit this. Now there is nothing that shows up here by default. 43 00:02:51,200 --> 00:02:54,000 Some of the IDEs will show that file when it's created, 44 00:02:54,000 --> 00:02:57,500 CodeLite doesn't. So what you can do is you can select the project 45 00:02:57,500 --> 00:03:00,800 right click and say open containing folder, 46 00:03:01,300 --> 00:03:04,660 that'll open it up in explorer or in finder if you're on a mac. 47 00:03:05,100 --> 00:03:07,100 And I'll just drag that over here. 48 00:03:07,100 --> 00:03:12,090 And you can see that's right here we just created the file name. Remember -- let me drag this down just a little bit. 49 00:03:12,090 --> 00:03:16,090 The file name was output.txt. There's output.txt right here. 50 00:03:16,490 --> 00:03:19,490 And you can see the contents of it right over here hello there file. 51 00:03:20,850 --> 00:03:23,850 Okay. Now let me move this out of the way, and let's run this again. 52 00:03:24,510 --> 00:03:26,010 And if we run it again, 53 00:03:27,210 --> 00:03:29,210 I'll just say my name is Frank 54 00:03:30,460 --> 00:03:31,560 and press enter. 55 00:03:32,660 --> 00:03:35,460 And now if we look at the file again, I'll drag this back over. 56 00:03:35,460 --> 00:03:38,860 You can see that what was there before was erased 57 00:03:38,860 --> 00:03:42,960 because we're opening the file in truncation mode so it gets rid of whatever was there. 58 00:03:42,960 --> 00:03:46,320 That's the default. Now if we want to add something to the file, 59 00:03:46,320 --> 00:03:50,520 we can simply open it in the pen mode. So we can put a another flag here and just say -- 60 00:03:50,880 --> 00:03:55,870 we'll say std ios append mode or app. 61 00:03:56,120 --> 00:03:59,720 And if we run this again now, we say this 62 00:03:59,720 --> 00:04:01,920 should be another line. 63 00:04:03,420 --> 00:04:05,780 Okay. So we'll take a look at the file now. 64 00:04:06,180 --> 00:04:09,680 And you can see my name is Frank, and now I put this should be another line. 65 00:04:10,180 --> 00:04:13,380 Okay. So you can see that it's actually appending to the existing file. 66 00:04:13,380 --> 00:04:16,380 So that's it. That's pretty straightforward and really that's the basics 67 00:04:16,380 --> 00:04:18,880 of output to text files. 68 00:04:18,880 --> 00:04:21,680 We have the power of all those stream manipulators, 69 00:04:21,680 --> 00:04:25,040 which really gives us a lot of power into how we format the output files. 70 00:04:25,040 --> 00:04:28,040 Okay. So what I want to do now is I want to switch to 71 00:04:28,590 --> 00:04:29,470 another project. 72 00:04:29,470 --> 00:04:34,270 It's going to be the CopyFile_1 project. So I'll switch to that, and I'll be right back. 73 00:04:36,070 --> 00:04:38,370 Okay. So I'm in the section 19 workspace. 74 00:04:38,370 --> 00:04:41,470 And I've opened up now the CopyFile_1 project. 75 00:04:41,470 --> 00:04:44,770 And in this project, what we've got is this poem. We've got 76 00:04:44,770 --> 00:04:49,030 Shakespeare's sonnet again. And what we want to do is we just want to copy it to another file. 77 00:04:49,030 --> 00:04:53,030 So what we can do is I've already written the code, and it's really really straightforward. 78 00:04:53,030 --> 00:04:56,930 It's just like it was in the slides. What we can do is we can start here. 79 00:04:56,930 --> 00:04:59,280 And you can see we've got our includes that we need, 80 00:04:59,280 --> 00:05:03,280 and we've got an input file stream called infile, which is my poem. 81 00:05:03,580 --> 00:05:06,460 And then I've got an output file stream called out file, 82 00:05:06,460 --> 00:05:11,120 which is my poem_out.txt that's the file we want to create 83 00:05:11,120 --> 00:05:14,120 that's going to be a copy of the poem.txt file. 84 00:05:15,670 --> 00:05:20,550 So again, we're basically following the recipe. We're instantiating those objects.We're making sure that 85 00:05:20,550 --> 00:05:23,750 our file streams are open and ready to read and write. 86 00:05:23,750 --> 00:05:28,110 And then we've got our logic. And our logic is really only just a couple of lines of code. 87 00:05:28,110 --> 00:05:33,010 We've got a line that we want to read file -- the file input into, and there's my loop. 88 00:05:33,450 --> 00:05:36,750 I'm using getline to read a line from infile. 89 00:05:37,550 --> 00:05:41,210 And when I get that line, all I'm going to do is write it to out file. That's it. 90 00:05:41,210 --> 00:05:44,510 At the end, we're going to display a little message that says file copied. 91 00:05:44,510 --> 00:05:47,810 And we're going to close both files. So if we run this, 92 00:05:49,410 --> 00:05:54,180 it'll say file copied. That's it. You can see how quickly this runs. 93 00:05:54,730 --> 00:05:59,090 And we'll open up that project, the containing folder. And we should see 94 00:05:59,090 --> 00:06:00,560 a poem underscore out. 95 00:06:00,560 --> 00:06:04,320 There it is right here that was just created 1k big. 96 00:06:04,320 --> 00:06:07,920 And if I select it, you can see that the poem is over here. 97 00:06:07,920 --> 00:06:12,120 This is the original poem, and this is the copied poem. So it's exactly the same. 98 00:06:12,120 --> 00:06:14,620 So it's really easy to copy one file to another. 99 00:06:14,620 --> 00:06:19,120 In this case, we used formatted input and output because we're using getline, 100 00:06:19,780 --> 00:06:23,340 and we're using the insertion operator with line to write it to the file. 101 00:06:23,340 --> 00:06:27,640 That's it. Now if we go over to CopyFile_2 project, 102 00:06:27,640 --> 00:06:28,640 let me open that up. 103 00:06:31,300 --> 00:06:36,180 It's exactly the same thing. The only difference here is that we're using get and put. . So we're using 104 00:06:36,180 --> 00:06:37,880 unformatted input output. 105 00:06:37,880 --> 00:06:42,130 And you can see that the code is exactly the same except the while loop and the 106 00:06:42,130 --> 00:06:47,030 variable declaration right there. That's the only difference is we're doing it one character at a time. 107 00:06:47,030 --> 00:06:50,020 So while we get a character from infile, 108 00:06:50,020 --> 00:06:53,020 we output that character to outfile. That's it. 109 00:06:53,270 --> 00:06:57,170 So let's run this. I'll select that, make sure that that project is selected. 110 00:06:57,570 --> 00:07:00,070 I'll run this. It's done. 111 00:07:00,470 --> 00:07:03,970 And I'll open up that project in the containing folder. 112 00:07:04,960 --> 00:07:07,220 And right here, you'll see poem.out. 113 00:07:08,100 --> 00:07:10,900 And it's right over here, you can see it right there. 114 00:07:12,780 --> 00:07:17,440 So that's it for this video. Here you can see a few examples of creating files, 115 00:07:17,690 --> 00:07:21,190 appending to files and copying files using unformatted and formatted io. 116 00:07:21,690 --> 00:07:25,690 Next is a challenge. And in the challenge what we're going to do is we're going to take 117 00:07:25,690 --> 00:07:30,190 Shakespeare's, Romeo and Juliet and add line numbers to it and create a new file. 118 00:07:30,550 --> 00:07:31,550 I'll see you there.