 Hello and welcome to this tutorial on C programming. Today we're going to write to a file. So let's go ahead. I'm in a directory here that is empty. I'm going to create a file. I'm going to use Vim as my text editor, but of course use whatever text editor you prefer. I'm going to call my C file just main.c. Actually, I'll call it, this is the 11th tutorial, write file.c. There we go. Okay, we're going to start off. We're going to include, oops, include our standard IOH. I like to have space here in there real quick and save and go back into that. Okay. That shouldn't have that. That's why it's not color code because it had that exclamation mark there. No exclamation mark there. Okay, we're going to make our main function here. So int main. And of course, we're going to return zero if this is successful since it's an int. Okay, so now we're going to create a file here. So we're going to say file space, asterisk, f, we'll call it for file. And so we've created that. Now we're going to say that's going to be equal to opening a file. So we're going to say f equals f open. And we'll create a file called test dot text. And we're going to say w for write. So what are we doing here? We're creating basically a variable called f here that it's a file variable. And we're saying, okay, we're going to open the file test dot text for writing to we're going to change that in a minute. I'm just going to show you two different options here. There's a few options there. But here, now we're going to print something into that file. So we're going to say f print f. And then we're going to print to that f variable. And we're going to say hello. And I'm going to do backslash and here. So it adds a new line character at the end of that we'll play around with that in a little bit. I'm going to say f close. And we're going to close so the f here. So again, we're creating f here and we're saying that it's this file, we're opening it for writing, we're going to print to that file, we're going to print hello with a new line. And then we're going to say close. So we're going to close the using f close function, we're going to close that f. And that's it. If we go ahead and save that we can now gcc our file here Oh, and we'll just call this the same thing but without the dot c. Oh, got to have semi colon at the end of our return there. That's all it's letting us know there. We'll try compiling it again. It's compiled now can dot slash that file. And we had no output in that file in that program. But if we list out files now you can see there is a test dot text and if I was to cat out that file to show the contents of it. Hello now if we run the program again and cut it out again, you can see that nothing has changed and actually did we overwrote what was in that file. We open it for writing basically we're creating a new file we're opening it and overwriting everything in there. If we want to append mean add to the bottom of that file. Well, we can do that too. If we go back into our C program here instead w we can change this to a which means append now if I recompile our program gcc our C file dash output 11 or where we want name our executable. Now I run that a couple times. And if I cat out, let's clear the screen. Let me type cat properly test dot text. You can see we now have a hello four times if I run it again. It has hello five times. Now again, if we go back into our program here, I have the new line character here. If I remove that new line character print f is not going to put a new line character at the end of that line. So if I was to compile it, I'm going to run it again. I'll run it a couple times and then I'll cut it cat it out. And so we're still appendage. So what was in there before these five hellos are still there. But that last line now has hello, hello, hello. And you can see there's no new line even at the end of the file, depending on how you want to pen to the file that may be important. Also, we can go in here and we can say copy that line a couple times. I can say hello, space world here. And I can say test, be sure to put the spaces I want and then new line. So now if I compile that, and I run it again, I'll run it a couple times, then I'll cat out that file. Oh, not that file. That's our executable cat test. You can see that it printed. Well, here printed hello world because there was no new line at the end of the file before. So printed here. And even though we printed that print f it on multiple lines in the program, because I only put a new line at the end of test, it's still putting their all on one line. That's how it came out. All types of awkward. But again, let's look at our file here, our script. 11.c. So even though we're print f'ing multiple times here into that file, there's no new line on these lines. So it will print to that same line until there is a new line character. Hope that makes sense. Let me just go ahead and remove those and go back to just our basic hello. And I will save it like that. Now again, you can get this executable and all the other ones in the story as well as a bunch of other scripts I've written. If you go to gitlab.com forward slash metal x 1000 forward slash capital my capital bin. And there you'll see these are just like a bunch of random scripts I've written over the years. And if you go into C, there's tutorials and all the programs we've worked on in this tutorial series are in here as well as the one we just created. So there you go. You can download those, modify them, play with them. You know, if you're having a problem, there's errors with your code. You can always download mine, check it out. And you can also visit filmsbychris.com. That's Chris the K. There's a link in the description there. You can search through all my videos. And I do thank you for watching. And I hope that you have a great day.