you are the best ... trust me your are the only video that actually solve my doubt in the entire youtube ... keep the work man lots of love from INDIA
@PortfolioCourses Жыл бұрын
I’m glad it was helpful for you, lots of love from Canada! :-)
@vlogtomejr.19192 жыл бұрын
This is a great tutorial to remove the trailing newline from fgets(), but this does not account for one edge case where the input exceeds the buffer size. For example, if the buffer size is 8 and the user inputs 8 or more characters, then the trailing newline character is not present. There are a few ways to deal with this issue, but here is one simple solution: char userInput[8]; ... int length = strlen(userInput); if (userInput[length - 1] == '/n'){ userInput[length - 1] = '/0'; length = length - 1; }
@PortfolioCourses2 жыл бұрын
That's true, this example assumes that the string entered will be less than the buffer size and therefore there WILL be a trailing . And yes doing something like this will handle that edge case. :-)
@DeanT.2 жыл бұрын
Thank you sir, i found this method from other video but didn't understand it. After founding this video i finally understand it.
@PortfolioCourses2 жыл бұрын
You're welcome, I'm glad to hear this video was able to help you out! :-)
@BLACKBERREST32 жыл бұрын
this helped a lot. I think i will combine this with an if statement if the user enters past the buffer amount “”i.e over-buffering attack” to check if there even is a trailing new line at all. from what i can gather, fgets() does not have a trailing new line if it runs out of buffer space and will instead end with the null terminator ‘\0’ at array[strlen(array)] & also a have a valid character you do not want overridden at array[strlen(array) - 1].
@PortfolioCourses2 жыл бұрын
You're welcome! 😀 And yes, that's the way fgets() will behave if buffer-size -1 chars have been read in with no newline being encountered.
@Avionics22 жыл бұрын
Thank you for clarifying this.
@PortfolioCourses2 жыл бұрын
You're welcome! :-D
@jimbo54372 жыл бұрын
YOU ARE A LIFE SAVER
@PortfolioCourses2 жыл бұрын
I’m glad to hear it helped you out Jim! :-)
@farhanlabibahan8 ай бұрын
excellent,, you soleved my problem perfectly,,,,thanks a lot
@Graxx19065 ай бұрын
ohhhh man thanks a lot for this video sir
@Solomon_Ayuba6 ай бұрын
I was searching for the use of the getchar(); function in consuming newline character when I found your tutorial. Can the getchar(); function work in your example above instead of the approach you used? (I'm a beginner at C 🙂)
@arthur_p_dent42822 жыл бұрын
I’m wondering if this is why I was failing my tests on code chef. The problem was to take S = a string of lowercase letters. Than take N words and output yes if you can spell that word using letters from S. Originally I was using fgets and failed. I thought way too hard about it. Eventually I just looked at someone else’s answer and switched fgets to just using scanf(“ %s”, string) and it suddenly passed.
@PortfolioCourses2 жыл бұрын
It sounds like this was the issue then, because scanf() won't leave in the trailing newline. :-)
@Sabari102511 ай бұрын
can't we use getch to remove the trailing character?
@udomtipparach48942 жыл бұрын
wonderful, Thank you !
@PortfolioCourses2 жыл бұрын
You're welcome Udom! :-)
@Waki369ify2 жыл бұрын
very helpful, thank u!
@PortfolioCourses2 жыл бұрын
You're welcome Park! 😀
@Rock-rk9hh2 жыл бұрын
what does pragrma code name 0x08 do?
@PortfolioCourses2 жыл бұрын
I think that's the backspace code? www.asciihex.com/character/control/8/0x08/bs-backspace But I'm not completely familiar with it myself. :-)
@Rock-rk9hh2 жыл бұрын
@@PortfolioCourses the teacher used it on pic18f4450. not sure why he wrote. i couldn't keep eye on everything
@mccauleybacalla22284 ай бұрын
TY!
@db9172 жыл бұрын
i tried this code, it its working only for 8characters, whenever you enter a string more than 8, it is not workin,can you please help
@PortfolioCourses2 жыл бұрын
The code for this example is found here: github.com/portfoliocourses/c-example-code/blob/main/remove_fgets_trailing_newline.c. I've tested it and it will work for strings of more than 8 characters. Maybe if you post your code in a comment here I can look at it.
@db9172 жыл бұрын
@@PortfolioCourses thanks alot for replying. maybe you can help, actually i implemented same code bc i need to use the same logic with ' ' and this is the closest thing i found to a solution. in my case i am reading the input strings from a file and the file has multiple parts. so i need to use ' ' to indacte that i am at the end of each part so i will move to reading the next part. i don't know if that makes sense. can you please help?
@PortfolioCourses2 жыл бұрын
@@db917 I'm not sure I can help in this case, that sounds like a pretty specific problem that you're trying to solve and I don't know all the details. Based on what you're saying though, it's possible these videos might help you with what you're trying to do: kzbin.info/www/bejne/eaukmIBrl8qtY8k kzbin.info/www/bejne/qnjKnWl7gr-rgrc
@moadelhaddad43762 жыл бұрын
Is there any possibility to do it with getc() ? Actually I'm working on a program that reads bits from a file but the problem is that it also reads the newline character.
@PortfolioCourses2 жыл бұрын
Hmm, getc() doesn't really read a whole line so I don't think this exact problem comes up with getc(). I guess what you're trying to do is stop at the newline character. If you have something like: char c; ..... c = getc(); if (c == ' ') insert null terminator into char array; that should work. You might have a counter variable keeping track of how many characters have been read and at that point you insert the null terminator into the char array to end the string. Basically read until getc() returns and then stop at that point. Hopefully this helps. :-)