2 00:00:00,000 --> 00:00:02,470 >> SPEAKER 1: Let's write a program that prompts the user for the names and 3 00:00:02,470 --> 00:00:03,990 houses of three students. 4 00:00:03,990 --> 00:00:07,300 But rather than just print out their names and houses to the screen, let's 5 00:00:07,300 --> 00:00:09,660 instead save that information to disk. 6 00:00:09,660 --> 00:00:14,530 In other words, let's create, say, a CSV file, for comma separated values, 7 00:00:14,530 --> 00:00:17,720 which is actually just a text file that programs like Excel or Numbers 8 00:00:17,720 --> 00:00:21,690 can open, and effectively save those names and houses permanently so that 9 00:00:21,690 --> 00:00:23,220 we can review them later. 10 00:00:23,220 --> 00:00:25,960 >> To do so, let's first take a look at some boilerplate code that I've 11 00:00:25,960 --> 00:00:27,650 started with already. 12 00:00:27,650 --> 00:00:32,380 Notice first that among the headers up top is now standard lib.h which 13 00:00:32,380 --> 00:00:36,710 happens to have some functions related to file I/O, file input and output. 14 00:00:36,710 --> 00:00:39,560 Notice that I've also declared a constant called STUDENTS-- 15 00:00:39,560 --> 00:00:42,380 in all caps-- whose value is hard coded as three. 16 00:00:42,380 --> 00:00:45,820 Notice now that inside of my main program, I'm declaring an array of 17 00:00:45,820 --> 00:00:50,270 size three using that constant called STUDENTS each of whose members is of 18 00:00:50,270 --> 00:00:51,650 type students. 19 00:00:51,650 --> 00:00:56,150 >> Recall now that a student we'll define as having a name and a house both of 20 00:00:56,150 --> 00:01:00,410 which are strings as per this declaration instructs, dot h. 21 00:01:00,410 --> 00:01:04,680 Now back in structs1.c, notice that I have a for loop here that's going to 22 00:01:04,680 --> 00:01:06,750 iterate from zero up to three. 23 00:01:06,750 --> 00:01:10,020 It's going to prompt me for a student's name and a student's house 24 00:01:10,020 --> 00:01:12,310 again and again and again. 25 00:01:12,310 --> 00:01:15,620 >> Then at the bottom of this program, notice that I have another for loop 26 00:01:15,620 --> 00:01:19,970 that's going to free the ith student's name and the ith student's house in a 27 00:01:19,970 --> 00:01:21,570 similarly constructed loop. 28 00:01:21,570 --> 00:01:24,480 To be sure, we could combine those loops into just one, but I wanted to 29 00:01:24,480 --> 00:01:28,180 have some distinct segments of code for the sake of discussion here. 30 00:01:28,180 --> 00:01:31,920 >> Now in between those for loops, let's actually have another, and this one's 31 00:01:31,920 --> 00:01:35,210 purpose in life is to actually save all of these names and houses that are 32 00:01:35,210 --> 00:01:39,810 currently in RAM to disk in the form of comma separated values. 33 00:01:39,810 --> 00:01:42,080 To do so, we're going to use three new functions-- 34 00:01:42,080 --> 00:01:47,450 F open, F printf, and F close, which open a file, print to a file, and 35 00:01:47,450 --> 00:01:48,440 close a file. 36 00:01:48,440 --> 00:01:49,690 We can use them as follows. 37 00:01:49,690 --> 00:01:52,110 38 00:01:52,110 --> 00:01:58,240 >> FILE, in all caps, which is somewhat of a curiosity in C, FILE gets fopen 39 00:01:58,240 --> 00:02:03,020 quote unquote and now a name for the file, say, students.CSV, but I could 40 00:02:03,020 --> 00:02:08,150 call it most anything, comma and now I'm going to specify a single w inside 41 00:02:08,150 --> 00:02:09,390 of double quotes. 42 00:02:09,390 --> 00:02:13,290 W, as you might have guessed already, means that fopen should open this file 43 00:02:13,290 --> 00:02:17,360 called Students.CSV for writing so that we can actually save some 44 00:02:17,360 --> 00:02:19,370 contents to it. 45 00:02:19,370 --> 00:02:23,080 >> Let's next check if file is not equal to null. 46 00:02:23,080 --> 00:02:25,860 Because if it is, something has probably gone wrong in which case we 47 00:02:25,860 --> 00:02:28,340 should not proceed to try to print anything to it. 48 00:02:28,340 --> 00:02:33,400 But if it's not null, then inside of the curly braces I'm going to iterate 49 00:02:33,400 --> 00:02:38,030 from i equals 0 on up to STUDENTS, and I'm going to 50 00:02:38,030 --> 00:02:40,180 increment i on each iteration. 51 00:02:40,180 --> 00:02:45,750 And inside of this loop, I am going to fprintf so as to print to a file-- 52 00:02:45,750 --> 00:02:47,940 specifically the one I already opened-- 53 00:02:47,940 --> 00:02:54,650 a string that looks like this %s,%s backslash n close quote. 54 00:02:54,650 --> 00:02:58,790 >> And now I want to plug in to each of those placeholders the actual values 55 00:02:58,790 --> 00:03:03,390 of a student's name and a house using the dot operator. 56 00:03:03,390 --> 00:03:10,030 Students bracket i.name, students bracket i.house close paren 57 00:03:10,030 --> 00:03:11,230 semi-colon. 58 00:03:11,230 --> 00:03:16,180 Now below this for loop, I'm simply going to call fclose of file in order 59 00:03:16,180 --> 00:03:18,520 to ultimately close the file. 60 00:03:18,520 --> 00:03:21,360 >> Now when I run this program, I shouldn't actually see anything on the 61 00:03:21,360 --> 00:03:25,010 screen, but I should have after running this program a file called 62 00:03:25,010 --> 00:03:29,130 Students.CSV in the same directory that I run the command in that should 63 00:03:29,130 --> 00:03:32,480 contain a comma separated list of values. 64 00:03:32,480 --> 00:03:34,790 Let's take a look. 65 00:03:34,790 --> 00:03:41,690 >> Make structs 1./ structs1 student's name, let's say David, 66 00:03:41,690 --> 00:03:43,140 he'll live in Mather. 67 00:03:43,140 --> 00:03:46,890 Student's name, let's say, Lauren, she'll live in Leverett. 68 00:03:46,890 --> 00:03:50,800 Student's name, let's say, Rob, he'll live in Kirkland. 69 00:03:50,800 --> 00:03:54,050 Now, again, as expected, nothing appears to have happened, but let me 70 00:03:54,050 --> 00:03:58,790 go ahead at the command prompt and type g edit students.CSV in hopes that 71 00:03:58,790 --> 00:04:00,850 that file indeed exists. 72 00:04:00,850 --> 00:04:07,010 >> Gedit students.CSV ENTER, and indeed, notice that a file containing purely 73 00:04:07,010 --> 00:04:11,320 text, but text separated by commas for each field, indeed exists. 74 00:04:11,320 --> 00:04:14,530 And if we were using, not the CS50 appliance, but a more familiar Mac or 75 00:04:14,530 --> 00:04:18,080 PC, it turns out that we could indeed open this CSV file with a more 76 00:04:18,080 --> 00:04:20,400 familiar program like Excel or Numbers. 77 00:04:20,400 --> 00:04:22,906