How to read a line of text in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so how to read the line of text in C we want to read a whole line that is typed on the keyboard by a user and we want to get that on line inside our program how can we do this let's first declare some variables so let's say text there's the text that we want to read let's allocate 100 characters for it let's print a nice message like type a line of text here okay and let's let's use the a function that you may have used in the past all right let's first try that let's just Kenneth and inside of scanf we use the string specifier and here we type in the text text since text is an array text itself is really a pointer to that all right so it's fine we can just type it in here we don't have to add an address to it it would be the same result but we don't have to do that don't worry about it and lastly let's try to print it on the screen you typed to type the line of sand s and here we go with that text and if I got to run this here's the program and let's let's type in I know something and hit enter yeah nice that works okay let's try something else let's try something more and hit enter let's see what happens oh well we have an issue here we only got something from the tape from the line and second word didn't get included right so even even though we actually printed the whole line of text okay no problem you could technically read every single word but what we want to accomplish here is read as many words as possible right up until the user hits Enter okay wait to do this with scanf is to use a special specifier here that says taking everything that is not a backslash N and XS n is the new line character and that means that and and it's added once you hit enter so that means that we're going to read everything up until that enter we hit so if we try to run this and taking something more hit enter we we get the line that we wanted something more that's great that's amazing but the problem here with this is that if you try to run this twice so let's say I'm gonna just copy and paste this whole thing just a text to here x2 and text to do the exact same thing we try to run this and we say something more something really bad happen and you won't notice it it's not obvious what happened here so let's also add indexes on here so if I try this again something more right so you type the line something more that's great that's that works but then it says type a line of text here and then just jumps to you type the line something garbled text what's happening here exactly well you see if the standard input is basically a buffer right so you have something that you take from okay and scanf says all right well take everything everything that is not that that's not in reg X so that is not a backslash n so it's gonna go okay well you typed in something more and then backslash n so I'm gonna just take everything behind Dexter n cool and now we have a backslash n inside the buffer left after this Kenneth now the issue is when the seconds kind of comes in here it says okay well let me get everything behind that scanf that back sir Shan and looks well okay here's the back search and let me take everything behind it well there's nothing there nothing to take there but scanf didn't take everything after the best end so the issue here is that the best question is left in the buffer and there's no way for scanf to know that that was they are beforehand all right so we have to take that back section from the buffer to accomplish this we can I'll be it a bit a little bit hacky we can simply say get char okay and what that will do that usually returns a character right from the buffer but we don't have to treat instantly signature right it says int C deckle and that intellectually the character in index mode all right so get char just get one character from the buffer and if I try to run this you will notice that let's say something more and something even more and you'll notice that we get both lines of text over there all right so this works this is a bit Hecky but you will have to do this every single time you read from scanf right okay that's that's a way to do it that's a way of reading the line of text from the input just on that input but a better way of doing this and that is recommended by everyone on the internet is to use a function so let me just I'm gonna comment this two guys out obviously understandable so there's a function called F gets so I've gets and this function has takes in three parameters okay first is where you want to put the string you want to read that is text next is how many characters you put in that text right you notice that in the scanf we don't specify that which means this string that we read can be as long as you want it's just going going to paste it here even though even though it might go past the memory allocated right we only allocated 100 bytes but what if we type in a thousand bytes well that those bytes are going to be saved after this variable here okay and in this case we don't suffer the same problem we can specify exactly how many characters we can read which is in this case a 100 characters okay and one more thing we have to specify since this is mostly for file reading operations we can't really use the STD in file descriptor it means take input from the standard input from the keyboard all right that's what this function is and now if I try to print this so let me just delete this whole thing if I try to run this says type a line of text here and if I say I say something more and hit enter you notice it works but there's an issue is there one one space in between this and this it looks a little bit different right you won't notice it unless you use the string somewhere else so let's iterate over the whole text that we read and see what's up okay so I'm just going to use a for loop here so for me to Karen I here I equals zero I less than STR line of text and I plot class and I also want to see the backs are zero at the end I want to make sure that it's actually there so let's say less or equal than STR Len right so weird we're not going over the sides were just iterating over the last element as well okay and then just printf that character so we go % % c as da % d and find out the there's a decimal value of the character all right so if we will text off I here and we also bring F a backslash n so that that's separated let's see what happens so if I type in here let's type a simple simpler line of text in a and hit enter and here are the values of each of the character inside that text variable all right well let's find out what all those means so I have here a ASCII table with me open and let's analyze this a little bit all right we have first the character 97 97 is where is it lets up to the right a little bit more aha it is all right is the lowercase character a that's cool okay that's great that that's what we gave it as an input okay and now we have so you have another 97 and that's repeated three times we know that then there's a ten what is 10 exactly well 10 is this LF or a new line feed or newline character right let's exactly this character that we have typed in so you can notice that F get also saves inside the text variable the enter right the enter that you pressed at the end of the text what you usually want to do is remove that backslash n by simply doing text off and let's see what what we want exactly well we will know that we know that STR land returns the number of characters be up to 0 but not including 0 right so we have STL n is 4 right so we want STR land of text which is 4 but the index is counting up from zero so we want minus 1 right this is zero character first second third so that's the third one that we want to replace with a vector to zero to do this you just simply say STR land of text minus one equals and you can type it in as simple codes single codes and Dexter zero or you can just use equal zero if you want it's exactly the same thing okay and now if I try to run this line of text let's see something or if I type in something more here you will notice that we don't get a ten at the end here right I want to get a hundred one which is probably that e out there let me just run this again with a a and hit enter as you can see with no longer have that ten in between 97 and zero we only have that zero so the best way to read a line of text in C is using this FGS function don't use this can have as it can cause a buffer overflow right you can specify the size or actually you can kind of specify it but it's a little bit janky and you will get yourself into more trouble if use that so use F get for reading a line of text where you can specify exactly how much you can store inside that variable and just replace the backslash n with a backslash 0 so keep this in mind whenever you want to read a line of text because I forget don'tjust can have don't use it's kind of percent s don't don't use this thing it's not it's not recommended to use this thing this is probably the best way to do it so thanks for watching and I hope you have learned something this lesson
Info
Channel: CodeVault
Views: 22,499
Rating: 4.8932037 out of 5
Keywords: reading text in C, reading strings in c, how to read a string, how to read a string in c, reading string in C, reading text in c, c (programming language), how to use fgets, how to use scanf, buffer overflow with scanf
Id: Lksi1HEMZgY
Channel Id: undefined
Length: 13min 22sec (802 seconds)
Published: Wed Jan 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.