'lo everybody...
When using fgets() is there a way to stop it adding the newline character to the end of my input?
Erm... that's all... any help is appreciated...
Thanks
'lo everybody...
When using fgets() is there a way to stop it adding the newline character to the end of my input?
Erm... that's all... any help is appreciated...
Thanks
Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx
It only takes one line to get rid of the newline
Code:fgets( buff, BUFSIZ, stdin ); // find the newline, and if its there, overwrite it with \0 char *p = strchr( buff, '\n' ); if ( p != NULL ) *p = '\0';
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Makes sense, thanks again Salem....
Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx
An alternative to Salem's code that does the same thing:
Code:fgets(buf, BUFSIZ, fp); buf[strcspn(buf,"\n")] = '\0';
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
strcspn?
That's a nice one Hammer. Thanks for showing the alternative.
>>Hammer, could you please stop advising people to use strcspn() instead of strchr()?
You didn't read my post properly then? I wasn't advising them to use it, only showing another way to do the same thing (just like I said).
And I'll post what I like, thanks.
>>There are a thousand ways of doing this and the 10 bytes (or so) of program text it saves aren't so significant that you have to convince everybody of using it
Again, I'm not trying to convince anyone. Knowing more than one day to do something is an advantage in itself.
>>(in fact, the strchr() version is faster ;P).
Really, now that I didn't know. Can you back that one up with some evidence (yes, I'm serious, this is not a sarcastic comment, I am interested to know which would be the better way).
>>That's a nice one Hammer. Thanks for showing the alternative.
No problem, at least someone appreciates it!
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
>I didn't mean to make a discussion of this
Why not, that's what these boards are for! I don't take your comments personally, and I promote open conversation
>...but strcspn() just can't be done as fast.
Thinking about it, the answer is obvious. With the second parameter of strcspn() being a list of chars, it's clear that the function will need to loop through them, whereas strchr() has to work with only one char.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I get 771, 1942 for VC++ 6.0, but this is without optimization. But seriously, if you have to run it 10000000 each to get these tests does it really matter which one is faster? Sometimes yea, but most of the time it's pointless, write a program that's clear and easy to read from the start and if there're speed issues then deal with them after the program is working. I've read all over the place that optimizing little things for almost no speed gain is stupid unless you need to. I know of four ways to do this now. Naturally I like the one that's the easiest to read and figure out without looking over more code than I have to.
Code:char *p = strchr( buff, '\n' ); if( p != NULL ) *p = '\0';Code:buff[strcspn( buff, "\n" )] = '\0';Code:if( buff[strlen( buff ) - 1] == '\n' ) buff[strlen( buff ) - 1] = '\0';/* Edited */Code:char *p = strpbrk( buff, "\n\0" ); *p = 0;
Last edited by Dr. Bebop; 09-11-2002 at 10:04 AM.
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6
I think this is a good discussion, not pointless. I'm learning a lot by watching you guys explain your take on things.
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6
He Dr. Bebop, you forgot one:
Code:char *p = strpbrk(buf, "\n\0"); *p = 0;
>>I think this is a good discussion, not pointless.
Maybe the subject does seem trivial, but it does promote a good convo! Which is more than I can say for a lot of threads (like "where can I find this...", and "how do I do that...")
>>Just a matter of personal choice
yes, exactly, which is how we started, by giving someone a choice.
>>I'm learning a lot by watching you guys explain your take on things.
Good
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
If there is no newline in the array will strcspn put a null terminator at the end of the array?
My manual says no. The two parameters passed in should already be null-terminated.
Try not.
Do or do not.
There is no try.
- Master Yoda
No, strcspn simply returns the number of characters in the first arg that are not in the second arg (starting from the beginning).Originally posted by Troll_King
If there is no newline in the array will strcspn put a null terminator at the end of the array?
So, if you do strcspn("hello", "o") the return will be 4. If no match is found at all, then the number returned is the number of chars in the array, not including the null terminator. Eg:
strcspn("hello", "x") will return 5.
This makes it an ideal function to put into the array [ ] to get at a specific index, as per the example discussed here.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
So that's why you can do the one liner? If there's a newline then it's relplaced by null and if there isn't then strcspn returns the index of the trailing null, so the string doesn't change? Cool
Processing error: Stupidity detected.
------------------------------
Dr. Bebop
Windows XP Professional Ed.
Microsoft Visual Studio 6