Remove Trailing Newline Character From fgets() Input | C Programming Example

  Рет қаралды 12,233

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 32
@SOUVIKMANDAL-s9q
@SOUVIKMANDAL-s9q Жыл бұрын
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
@PortfolioCourses Жыл бұрын
I’m glad it was helpful for you, lots of love from Canada! :-)
@vlogtomejr.1919
@vlogtomejr.1919 2 жыл бұрын
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; }
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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.
@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.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome, I'm glad to hear this video was able to help you out! :-)
@BLACKBERREST3
@BLACKBERREST3 2 жыл бұрын
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].
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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.
@Avionics2
@Avionics2 2 жыл бұрын
Thank you for clarifying this.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome! :-D
@jimbo5437
@jimbo5437 2 жыл бұрын
YOU ARE A LIFE SAVER
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I’m glad to hear it helped you out Jim! :-)
@farhanlabibahan
@farhanlabibahan 8 ай бұрын
excellent,, you soleved my problem perfectly,,,,thanks a lot
@Graxx1906
@Graxx1906 5 ай бұрын
ohhhh man thanks a lot for this video sir
@Solomon_Ayuba
@Solomon_Ayuba 6 ай бұрын
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_dent4282
@arthur_p_dent4282 2 жыл бұрын
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.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
It sounds like this was the issue then, because scanf() won't leave in the trailing newline. :-)
@Sabari1025
@Sabari1025 11 ай бұрын
can't we use getch to remove the trailing character?
@udomtipparach4894
@udomtipparach4894 2 жыл бұрын
wonderful, Thank you !
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Udom! :-)
@Waki369ify
@Waki369ify 2 жыл бұрын
very helpful, thank u!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome Park! 😀
@Rock-rk9hh
@Rock-rk9hh 2 жыл бұрын
what does pragrma code name 0x08 do?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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-rk9hh
@Rock-rk9hh 2 жыл бұрын
@@PortfolioCourses the teacher used it on pic18f4450. not sure why he wrote. i couldn't keep eye on everything
@mccauleybacalla2228
@mccauleybacalla2228 4 ай бұрын
TY!
@db917
@db917 2 жыл бұрын
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
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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.
@db917
@db917 2 жыл бұрын
@@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?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@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
@moadelhaddad4376
@moadelhaddad4376 2 жыл бұрын
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.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
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. :-)
Be Careful When Using scanf() in C
12:22
NeuralNine
Рет қаралды 131 М.
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
fgets is unsafe! So what’s the alternative?
11:32
Code With Huw
Рет қаралды 3,8 М.
File Access Basics | C Programming Tutorial
24:05
Portfolio Courses
Рет қаралды 109 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 343 М.
Reading/Writing structs to files (aka Serialization)
14:41
CodeVault
Рет қаралды 77 М.
How to use scanf with fgets
8:59
CodeVault
Рет қаралды 34 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 914 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Learn Any Programming Language In 3 Hours!
22:37
Code With Huw
Рет қаралды 638 М.
"Clean" Code, Horrible Performance
22:41
Molly Rocket
Рет қаралды 945 М.
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН