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; }
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. :-)
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].
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 🙂)
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.
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.
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. :-)
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.
@@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?
@@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: th-cam.com/video/CzAgM5bez-g/w-d-xo.html th-cam.com/video/tGgl6EMZxLU/w-d-xo.html
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
I’m glad it was helpful for you, lots of love from Canada! :-)
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;
}
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. :-)
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].
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.
Thank you sir, i found this method from other video but didn't understand it. After founding this video i finally understand it.
You're welcome, I'm glad to hear this video was able to help you out! :-)
Thank you for clarifying this.
You're welcome! :-D
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 🙂)
YOU ARE A LIFE SAVER
I’m glad to hear it helped you out Jim! :-)
excellent,, you soleved my problem perfectly,,,,thanks a lot
ohhhh man thanks a lot for this video sir
can't we use getch to remove the trailing character?
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.
It sounds like this was the issue then, because scanf() won't leave in the trailing newline. :-)
你的英语好标准🤗🤗
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.
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. :-)
what does
pragrma code name 0x08 do?
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. :-)
@@PortfolioCourses the teacher used it on pic18f4450.
not sure why he wrote. i couldn't keep eye on everything
very helpful, thank u!
You're welcome Park! 😀
wonderful, Thank you !
You're welcome Udom! :-)
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
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.
@@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?
@@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:
th-cam.com/video/CzAgM5bez-g/w-d-xo.html
th-cam.com/video/tGgl6EMZxLU/w-d-xo.html
TY!