>> SPEAKER 1: Let's write a program that prompts the user for the names and
houses of three students.
But rather than just print out their names and houses to the screen, let's
instead save that information to disk.
In other words, let's create, say, a CSV file, for comma separated values,
which is actually just a text file that programs like Excel or Numbers
can open, and effectively save those names and houses permanently so that
we can review them later.
>> To do so, let's first take a look at some boilerplate code that I've
started with already.
Notice first that among the headers up top is now standard lib.h which
happens to have some functions related to file I/O, file input and output.
Notice that I've also declared a constant called STUDENTS--
in all caps-- whose value is hard coded as three.
Notice now that inside of my main program, I'm declaring an array of
size three using that constant called STUDENTS each of whose members is of
type students.
>> Recall now that a student we'll define as having a name and a house both of
which are strings as per this declaration instructs, dot h.
Now back in structs1.c, notice that I have a for loop here that's going to
iterate from zero up to three.
It's going to prompt me for a student's name and a student's house
again and again and again.
>> Then at the bottom of this program, notice that I have another for loop
that's going to free the ith student's name and the ith student's house in a
similarly constructed loop.
To be sure, we could combine those loops into just one, but I wanted to
have some distinct segments of code for the sake of discussion here.
>> Now in between those for loops, let's actually have another, and this one's
purpose in life is to actually save all of these names and houses that are
currently in RAM to disk in the form of comma separated values.
To do so, we're going to use three new functions--
F open, F printf, and F close, which open a file, print to a file, and
close a file.
We can use them as follows.
>> FILE, in all caps, which is somewhat of a curiosity in C, FILE gets fopen
quote unquote and now a name for the file, say, students.CSV, but I could
call it most anything, comma and now I'm going to specify a single w inside
of double quotes.
W, as you might have guessed already, means that fopen should open this file
called Students.CSV for writing so that we can actually save some
contents to it.
>> Let's next check if file is not equal to null.
Because if it is, something has probably gone wrong in which case we
should not proceed to try to print anything to it.
But if it's not null, then inside of the curly braces I'm going to iterate
from i equals 0 on up to STUDENTS, and I'm going to
increment i on each iteration.
And inside of this loop, I am going to fprintf so as to print to a file--
specifically the one I already opened--
a string that looks like this %s,%s backslash n close quote.
>> And now I want to plug in to each of those placeholders the actual values
of a student's name and a house using the dot operator.
Students bracket i.name, students bracket i.house close paren
semi-colon.
Now below this for loop, I'm simply going to call fclose of file in order
to ultimately close the file.
>> Now when I run this program, I shouldn't actually see anything on the
screen, but I should have after running this program a file called
Students.CSV in the same directory that I run the command in that should
contain a comma separated list of values.
Let's take a look.
>> Make structs 1./ structs1 student's name, let's say David,
he'll live in Mather.
Student's name, let's say, Lauren, she'll live in Leverett.
Student's name, let's say, Rob, he'll live in Kirkland.
Now, again, as expected, nothing appears to have happened, but let me
go ahead at the command prompt and type g edit students.CSV in hopes that
that file indeed exists.
>> Gedit students.CSV ENTER, and indeed, notice that a file containing purely
text, but text separated by commas for each field, indeed exists.
And if we were using, not the CS50 appliance, but a more familiar Mac or
PC, it turns out that we could indeed open this CSV file with a more
familiar program like Excel or Numbers.