Writing to text files in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
into this video we'll take a look at ways you can read and write to files in C so to start off let's think about a simple problem let's see we have a struct here I'm going to find simple struct just hold point and here I'm going to have inside this struct just two integrals x and y so that those denote the position of that point all right so there's my struct and what I want is sort of to preserve the state of a variable of that type basically so I have a point let's say your point let's call it p1 and this guy has the values 12 and let's say negative 9 but so I want this value to be to be preserved in a file and to be able to read it from that file using this same program so what this program is do is going to write this point to a file and then read it and display it on the screen that's all we have in mind for today so there are at least two ways you can write to these files first is just simple plain old text right you can write any type of text you format it however you want with however the limiters and it's going to be there and when you open that file it's gonna be nice and fancy in any of in any editor second way is to write in binary quite literally taking the memory of our part of the memory of a program and copy and pasting it on the disk on the hard drive or SSD or whatever whatever you have it basically on that file itself right so when you open that it sort of be looking the same in today's video we'll take a look ways you can read and write two text files right because this is already fairly complicated and the next video we're going to take a look at binary files as well so first things first we have to define some file handles to open for reading and writing right so simple enough we can say here file with all caps file pointer in and file pointer out when these are basically just sort of keys or handles however you want to call them to that file it's just simply a number that they're going to store in here anyway with these we can basically open and close any any file we want and read that and write to that using special functions so first things first let's open the file for writing because we first want to write and then read it so I'm gonna say F open underscore s in my case because I'm using a very recent compiler and also my editor is sort of forcing me to use the underscore s version of F open but if you don't have a recent companion it doesn't have this function no worries you can just use the F open command like anybody else so the first parameter is the actual place you want to store the handle where is that while we have defined it here right and to denote a place in which we want to store a file pointer pointer we have to pass along a double pointer to file to this type def here so same point of what we do is just get the address of our out because we first want to open the file for writing next up second parameter is the file name I'm going to call it point pump that you can use any sort of extension here some people you've been some people use B you can use txt windows or Linux doesn't even care but I'd like to use that just to you know that ok there's some data in there that's important not just a text file of nothing right and then the last parameter is the mode in which we want to open it well here you can say do I want to open it for reading or for writing basically that's what it asks me and what I want is to open it for writing so I'm gonna say W from write nights now after we have opened this file we should first check if it actually opened because maybe it just wasn't allowed to create a file there or something like that right if you don't have permissions to do that if your term doesn't have permissions to either create or write to this file it's not gonna work how do we know that well out this guy is going to be 0 or null so you can check that you can say if out is null then I'm just going to return one here all right so as you probably have seen from a previous video about the main function I'm telling you here it means it's successful and one it's just that something went wrong we don't really specify what but it's not really a problem for us right now you might want to print something on the screen for your problem maybe so now that we've checked we can use this we can use this out file handler to write to our file so what do we do we first want to write this p1 but you notice that p1 is actually a point it's not a string dot I see string so we have to first convert that to a C string how do we do that simple there's a function called it's similar to printer remember how printf takes in a specifier and multiple variables and just kind of cobbles it together in a string but that guy just print out on the console well there's a variant of it that says no no no don't print that string on the console save it somewhere for me so to do that we have to first define that somewhere we have to first define those strings that we're going to use and define here a few buffers so buffer in to be a basically C string of or a char array of 256 characters and buffer out with the same size right no particular reason why pick 256 you could have used anything you wanted okay so now we can use that printf variant can save s print F and E monkeys have to use the underscore s version right same reason as before and this guy takes him first is the buffer so simple enough we need the buffer for outputting that's why I call it out we need the buffer count so how big is that buffer well we know it's 256 bytes long so we're just going to paste it in there and then there's the format likewise we've printf so we have to say here a simple format in which we're gonna save this point and I'm simply gonna use basically two integers separated by a comma so that they look nice inside an editor right I'm going to also add a backslash and at the end of the line nice and then I just have to pass it the actual values and I have to say here P 1 dot x + P 1 dot Y so now we basically have created this string which is set of % DS it has the actual values on it and it's saved inside buffer out now what we have to do is take this buffer out and send it to the file and right there how do we do that simple we just use the F write function that just takes in our buffer a buffer out now here you can see that we have both I know it's a bit small but we have the element size and element count well we're writing characters here so you can say that the element size is just size of character or literally just 1 and element count is 5 or many characters we have inside our string well that's just STR land of buffer right because this these numbers can be however many digits long they can be so we don't know exactly and we don't want to write the whole 256 or a buffer to the file because there's no reason to do that right so just kind of limit it to this STR line and the last parameter is of course the file handler that were to the file that we want to write to which is out in our case right it's this guy that we have opened in the past now if I try to run this nothing happened of course because we didn't read anything we didn't print out anything on the screen but what I can do is go here on the resource files and add an existing item and go here and say point dot that and now this guy has been created if I click on it you'll notice we get the string here on the screen which is very very very nice we get both x and y coordinates that we have saved here so that's the first step we have written to the file nice now as this video is getting pretty long I can actually split it into two parts where we're going to take a look at reading from this file that we have made in detail so just to recap right what we have here is a point which is a user-defined type has just x and y we have to file handlers we just are you worrying so far just the out one we're not doing anything with this guy so I have this out which we open with F open underscore s or F open you just kind of have to be careful with the parameters when you're using just F open doesn't really need this guy it actually returns the pointer instead of taking it as a parameter but that's that's the only real difference there check if the file has been open and created of course it's going to create it for you as you might have noticed I didn't really create it previously this wasn't there so once I wrote to it it actually created it on the fly so that's very nice don't forget to check this then to really write this string to the file since we want a text file we have to have some text so we need this buffer out on this buffer out we use this special function called s printf that basically formats it as you can see so from here on what is like the parameters of printf it formats this like printf but it doesn't print it on the screen what it does is instead output the formatted string to our buffer right so we now have a string that contains these this this cool format for our point basically represents the data inside our point variable and then all we have to do is just call F right and F right said okay well that's right to this to this to this file so you're just gonna say buffer out size of chars or remember the element size and element count doesn't really matter as long as they when multiplied they actually come to be as long the amount of bytes you want to write from this buffer out right so this is we're just writing characters here and then lastly the output file handler and as a sort of good measure we should actually check if we read of files or not files bytes wrote here this guy F right is going to return it's going to return the number of bytes that it has written on the screen so we can do simply here if bytes wrote equals well this many right then we know that it's okay so if it's not that then we're going to Street on one right I'll try to run this again we get again exited with code zero so we know that we it went here so it didn't actually fail at this point and also at the end here we should also close the file using the F close function here which is going to give it the out file handler right so what once we wrote all this right before checking I should really close this file cuz otherwise it just stays there and it's being opened and then if I try to open it again for reading for example it's after work so just keep that in mind and I'm doing this here before the checking because I'm it's a possibility that we're not going to read or not we're not gonna write everything to it it's going to return and if it's here we're no longer gonna close it when it should always close your files so thank you guys so much for watching be on the lookout for the next part of this video series we're going to take a look at how to read this information and I'm going to take a look at binary files themselves and what are the advantages and disadvantages between these two so I hope you got something out of it and well if you have any questions do leave them down comments below or on our discourse server take care bye
Info
Channel: CodeVault
Views: 12,090
Rating: 4.8755555 out of 5
Keywords: fopen, fwrite, fclose, write to files, write in C, text files in C, codevault, serializing in C
Id: da9D4Bcsrgc
Channel Id: undefined
Length: 14min 32sec (872 seconds)
Published: Fri Sep 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.