No video

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

  Рет қаралды 10,608

Portfolio Courses

Portfolio Courses

2 жыл бұрын

Remove the trailing newline character in string input from the user received using fgets() in C, including a visualization of what is happening to help understand why the code works. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

Пікірлер: 30
@vlogtomejr.1919
@vlogtomejr.1919 Жыл бұрын
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 Жыл бұрын
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. :-)
@user-kc4so3lg3r
@user-kc4so3lg3r 7 ай бұрын
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 7 ай бұрын
I’m glad it was helpful for you, lots of love from Canada! :-)
@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.
@Avionics1958
@Avionics1958 2 жыл бұрын
Thank you for clarifying this.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You're welcome! :-D
@DeanT.
@DeanT. Жыл бұрын
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 Жыл бұрын
You're welcome, I'm glad to hear this video was able to help you out! :-)
@farhanlabibahan
@farhanlabibahan 2 ай бұрын
excellent,, you soleved my problem perfectly,,,,thanks a lot
@Solomon_Ayuba
@Solomon_Ayuba Ай бұрын
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 🙂)
@jimbo5437
@jimbo5437 Жыл бұрын
YOU ARE A LIFE SAVER
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m glad to hear it helped you out Jim! :-)
@Sabari1025
@Sabari1025 5 ай бұрын
can't we use getch to remove the trailing character?
@arthur_p_dent4282
@arthur_p_dent4282 Жыл бұрын
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 Жыл бұрын
It sounds like this was the issue then, because scanf() won't leave in the trailing newline. :-)
@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
@moadelhaddad4376
@moadelhaddad4376 Жыл бұрын
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 Жыл бұрын
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. :-)
@udomtipparach4894
@udomtipparach4894 Жыл бұрын
wonderful, Thank you !
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Udom! :-)
@Waki369ify
@Waki369ify Жыл бұрын
very helpful, thank u!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Park! 😀
@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
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Get 10 Mega Boxes OR 60 Starr Drops!!
01:39
Brawl Stars
Рет қаралды 14 МЛН
Meet the one boy from the Ronaldo edit in India
00:30
Younes Zarou
Рет қаралды 10 МЛН
Running With Bigger And Bigger Feastables
00:17
MrBeast
Рет қаралды 79 МЛН
the cleanest feature in C that you've probably never heard of
8:13
Low Level Learning
Рет қаралды 133 М.
fgets is unsafe! So what’s the alternative?
11:32
Code With Huw
Рет қаралды 2,8 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 205 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 351 М.
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 395 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 59 М.
How IDisposable and Using Statements Work Together in C#
10:01
IAmTimCorey
Рет қаралды 30 М.
int *p vs int* p Pointer Declarations | C Programming Tutorial
6:12
Portfolio Courses
Рет қаралды 135 М.
How To Return An Array From A Function | C Programming Tutorial
13:01
Portfolio Courses
Рет қаралды 65 М.
How to use scanf with fgets
8:59
CodeVault
Рет қаралды 32 М.
Get 10 Mega Boxes OR 60 Starr Drops!!
01:39
Brawl Stars
Рет қаралды 14 МЛН