[Music] one of the most common things that uh people who write C code do is um open up a file take data from that file and analyze that data that's might be one of the most common programming tasks that's someone in a stem field might do so uh in this video we're going to learn how to open a file and read data from it that's uh a really powerful tool for sure as always we're going to start by going down to the search bar uh type out CMD up will come the command prompt you click on that and then CD desktop CDC code and then we're going to use our our batch file uh make project space average of average uncore ofor file that's our going to be the program that we're going to write today um and um we did we need to do one extra ra thing here um is uh notice this command we're we need to make a data file you can either do this in notepad++ just by file new and all that stuff but I'm I'm going to do it at the command prompt just to show you how to make an empty file which is this this um echo. greater than symbol space data file.txt so data file.txt that's the thing we're going to open up and read from okay so in file manager you open that up and you uh double click on on the C file in this new folder average of file and up and notepad++ should come up um I think you can do the same with the text file but notepad or some other um some other um app might open it up so I would manually open up data file.txt with notepad++ just use file open from notepad++ to get the data file um I want to use the average scan f program that we did in the last video as my template so go open that up uh contrl a contrl c that whole program and then go into average uncore ofor file. C and then do contr a contrl v to have that as your starting template um okay so what's different about this program that um I have that's going to open up a file there's a new type of variable the variable is called a file all caps f i l e c is case sensitive so it's got to be all caps space then there's this star in front of it star FP the star has to be there star and and that's a whole conversation so it it's coming uh for right now star FP it's just a special kind of variable FP file pointer is what FP stands for and then um you never see the star again when you use FP right you're just going to say f P equals F open you're going to use the F open command which means file open and the first argument of f open is the name of the file in double quotes data file.txt comma and then the second file second argument in double quotes is what how do you want to open it do you want to open it as a write only as a read only as something else and so R means read a side note here if you open it up as a write file uh it'll wipe out whatever you did in another app and it'll just write it out to make it a blank file so uh C's a little bit um unforgiving of of it's not Idiot proof it doesn't um error check for you you have to do that stuff uh manually so be careful always almost always we're just going to read a file that's we are going to write some files but mostly reading so R for read then inside the for Loop the scan F statement the for Loop that was in the regular scan statement the the scanf statement is now an F scan f file scan scan from aile file scan F from a file is what that means and the first argument is not the double quote things it's the it's this FP variable right so FP comma then the rest is the same uh double quotes percent G comma Ampersand a subi below that I have a print F statement a diagnostic print F statement just to make sure that the data loaded okay that's you don't normally need to do that that's just to help me see the make sure that the program worked okay okay command s i control S I save that uh and um then I um I'm going to go into the data file.txt file and enter just 10 numbers I just randomly made 10 numbers there uh you make any 10 numbers you like and then I go back into the command prompt and I compile and run the program and you can see that the the index and the numbers all come up and the average value and you can Cal manually calculate the average or calculate it in Excel and verify that it's the right answer but yeah it's it gives you the right average value so it worked no problem and then if you look at here at the data file I'm just going to do a line by line comparison to see yes it did load the data correctly from data file.txt that's it that's all you need to do there's one other thing is that you need to do the fclose command I I think I forgot to do that in this program but um we're going to talk about F close too so at the bottom of every after you're done reading all the data points in you need to fclose open parenthesis FP uh that's a thing you have to do is just a housekeeping it doesn't break the computer for a while but if you keep doing it and you keep doing if you keep open files and not closing them at some point it can cause some issues uh so it's a good idea to close a file after you open it