How to use scanf with fgets

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i'm gonna take a look at why sometimes f gets actually fails to read anything from the keyboard or actually seemingly fails to read anything from the keyboard let's start with a simple problem let's say we want to hypothetically read messages from the keyboard and then send them to somebody and we also want to read before sending the message we also want to read an id representing that message so we want to read an id and then the message itself and instead of sending it to someone we're just going to print it on the screen sounds good let's go so we're going to have here an id and then we're going to have a message so i'm going to say message 256 all right that should be fine and then let's say we do this forever so i'm going to do a while through here and first i guess it's a good idea to just print f a message um let's say input id in the space then we're going to use scanf here because we want to read a number so that kind of makes sense right we do that scanf and then we say okay id and then printf input message and then of course here another well it's it's going to be another scanf because we want to read a string right so i'm going to say percent s this time and i'm just going to say message okay fine that should work now what happens if i print f let's say let's put in parenthesis id and say send send message percentage something like this should be the format and in here i'm gonna give it have uh the id and the message now let's see what happens let's try to execute this program if i launch it well i'm gonna get input id let me actually make this a bit bigger so let's say id1 because it's the first message and then i type in i don't know let's say test and i hit enter oh that nicely works so i have id1 here to the left and then the message sent message test nice now what if i say okay two and i want to set uh the message to be test message instead what's gonna happen if i hit enter now oh something really bad happened and what exactly did happen well the first obvious thing is that we actually get the id so the id was read properly but then the message did not get read because here we sent message tests we didn't actually get this message word out right and to make sure that this actually is what's happening i'm gonna refresh and restore uh restart the program and i'm gonna type in here something else so maybe something else and if i hit enter i can see the same exact behavior so i have something as the word here in the message right in the message variable but the else is nowhere near to be found and actually you can see it it's in here but i didn't type in two messages and i only typed in one and id got skipped what's going on there well you see scanf when reading strings is kind of not that smart in that it reads a string up until it finds a white space or a backstage and or an eof for example so in my case when i wrote something else this scanf actually actually only read the something right it's only read part of the message and then the rest got left inside the standard input buffer uh this else was left there and then it went back to this sec to this first scanner it tried to pass something from it it failed because it else is not a number and then of course it uh got sent to the second scanf and this is why we get this on the screen so how do we solve this issue well the simplest way to solve this issue is to use something else scanf we're going to use here f games and f gets i made a few videos on f kits you can check up top but fcs just gets uh a whole line of text up until a backslash n or an eof is found right so it doesn't it ignores uh white space this is perfect for us and we just have to pass in the message the max number of characters we're gonna read that's well that's just gonna be our size here and of course standard input as the input itself fkts can read from uh files as well by the way all right amazing now let's try the program again hopefully this is now solved if we try to do this again well let's see i'm gonna give it first the test and oh it already doesn't work i hit enter and we got the input message and the sent message printed on the screen already even though i didn't send anything i just hit enter why is that well in this case it's a bit more tricky and it has to do with the way scanf actually works again but with numbers or really any sort of format specifier for that matter and that is that scanf actually leaves the the enter character the backstage end that you left that you're left in in the input when you hit enter it's going to leave it inside the standard input right so in here we didn't just input one we actually input one and then a backslash n right and what scanf did is take this one from the standard input but that's it it didn't take that backstage n so f gets comes along and it sees this scanf and it's like oh well i mean we're done reading right because we are going to read a a line of text since we're going to just read a line of text there's nothing else to be read therefore well here we are we just get nothing on the screen so this returns or really sets no characters inside this message it's like it got skipped and nothing gets printed on the screen and this happens every time if i hit two here again it's gonna happen and that's not amazing now really easy way to actually solve this issue is to either uh call f get c or really get c onto the standard input or just thread with f gets another time and assume that it reads nothing here so if i do this instead before actually uh calling f gets right and i do it of course after reading from the scanf then if i restart this program come on there we go if i type in one and hit enter you'll notice that now we actually are able to type in a message and we can type in here any message with any number of spaces in between the words of course and hit enter and it's gonna be uh printed properly on the screen now of course as i told you in a previous video um f gets does store also the backstage and inside the resulting screen as a string and that is why we get here or what an empty line to remove that all you have to do is just say message of sdr lan of message minus one equals zero right and now if i restart a final time if i type in the id type in something else i'm gonna get just that message and no new line at the end as we would expect and then i can do the same thing just say uh this is a test hello and i hit enter and this works nicely so this is what you have to do if you want to use both f gets and scanf or rather if you want to use a scanf but also you want to read a line of text without taking into consideration the white spaces so you want to also have them in the string right this is the proper way to do it i think and uh it's it's much more safe than using scanf with percentage of course and that's it i hope you got something out of this video if you do have any questions leave them in the comments below or on our discord server again the source code for this lesson can be found on our website link in the description below take care bye you
Info
Channel: CodeVault
Views: 5,180
Rating: 4.9829059 out of 5
Keywords: codevault, c (programming language), scanf, fgets, gets skipped
Id: lXYSW4_ojEk
Channel Id: undefined
Length: 8min 59sec (539 seconds)
Published: Fri Jan 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.