Our next topic is a big one. It makes programming way more useful to be able to hook your programs up to the file structure of your machine. So what we'll be talking about is the basics of using C for File I/O. Let's assume you have a data file of homework scores, and however you got that file you could have created it yourself using your editor where you entered a bunch of scores. That would be typical as one of the ways you're using your computer. It can store a large amount of data and it can store it electronically. Let's say you want it average, maybe you even want the maximum minimum of those scores. So here's a data file. It has a bunch of homework scores. Maybe you create it in something called myhomework_scores. Now if we already had the data inside a C array. Let's say we had a C array of data with the appropriate number of scores. Then we would know how to process it to get the average. That's a fairly straightforward algorithm for us already. Though if you've made it this far in the class, that should be an easy exercise. So simple for loop. But now we want to get that data inside the program from a file. The way we're going to do that is with the function in standard I/O which is called fscanf. So normally, we could read from typing in a bunch of homework scores from our keyboard. But if they're already stored in a file, the way we access it is through what's called a file pointer to fp. So that's the first argument of scanf. Then the second third arguments are the normal ones namely; a conversion string, a format string, and arguments. So we already know how to use scanf. So this is just like scanf, but it needs a name file which will be in the case of what we just saw, my homework scores. Scanf normally assumes stdin, so in a way, it implicitly has such an argument already. An fscanf clearly is more general. Now in order to make use of a file, while it sits there, it could be locked or protected. So we have to give it a command that says "Open the file for reading '" because we want it for input. So there are various commands. There's a read command which is r, there's a right command which is w, and there's an append command which is a. You can also combine those commands. So here we see there's a special function called file open, fopen, again in standard I/O. It can take two arguments, namely the name of your homework. So if my homework was in my homework or if it'd been in my homework scores, this would be a longer name. So that's one string. Then the second string is the kinds of permissions that the system is allowing on that file. In this case, since we want it for input, read is appropriate. We need a declared input file program. Let's call it ifp. Then we would call fopen with that, and that would assign that to ifp which is a pointer, which then would be used as an argument for fscanf for example. Now, files that are opened, there is a limit to how many the system will allow a particular user to open. I forget what the standard limit is, but I think it's in the neighborhood of 20 or less. So if you're going to be opening files and using them, you also want to close file such as good resource hygiene. If you get to the end of the program, it will close all your opened files anyway. But it's just a good habit to use the program fclose also on the input file pointer in this case, and then that would close down that program, you no longer have access to that data file. The next segment I'm going to actually write the code to do most of this. So you'll see it directly in code and you can modify that code to your own something that you can play with. If you play with that code, it'll give you again 95 percent of what you need to know about File I/O.