Consider what happens when EOF is sent with no characters. len is -1, because strlen(buf) is 0, and you get an infinite loop because there's no \n and the stream is EOF.Originally Posted by WaltP
Consider what happens when EOF is sent with no characters. len is -1, because strlen(buf) is 0, and you get an infinite loop because there's no \n and the stream is EOF.Originally Posted by WaltP
Good point... Although EOF is a little odd to enter from the keybord. Yes I know it's possible, but most users don't know how...Originally Posted by cwr
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
So don't forget to check for a successful call to fgets as well. And remember to consider piping.Originally Posted by WaltP
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
this is more than enough.Code:vehicle_make[strlen(vehicle_make)-1]='\0' ;
a newline character is considered a valid character and it can end the reading but in anycase A null character is always appended at the end of the resulting string.
if a newline is there vehicle_make[strlen(vehicle_make)-1] will contain the newline else it shall contain the default null character.
Last edited by qqqqxxxx; 03-06-2006 at 12:49 AM.
It's actually a little too much. If the newline isn't there, a non-null character will be there -- and you overwrite it anyway.Originally Posted by qqqqxxxx
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Didn't we already cover that? Since Prelude hasn't said anything, I'll steal this solution:Quzah.Code:vehicle_make[ strspn( vehicle_make, "\n" ) ] = '\0';
Hope is the first step on the road to disappointment.
You mean strcspn?Originally Posted by quzah
No, I meant strspn, which is why I used "\n". But that'll work too. :P (Actually I have never had occasion to use either one, which is why I never remember their names.)
Quzah.
Hope is the first step on the road to disappointment.
The strspn() function calculates the length of the initial segment of s which consists entirely of characters in accept. So strspn("foo\n", "\n"); would return 0, since f is not \n.
Well that'll teach me (not really, but it should) to post tired. I read that backwards. Yeah, strcspn is what you want.
[edit]
I was thinking the pair of them were along the lines of strstr and its counterpart, strchr. Where one works with a string to search for, and one just searches for a single character.
[edit]
Quzah.
Last edited by quzah; 03-07-2006 at 03:51 AM.
Hope is the first step on the road to disappointment.
Wow, haven't been here since I thought I had a working solution though I see there's been quite some uproar about this question . I've replaced the 'strlen line' with the strcspn one as it seems to be a more watertight solution. Thanks again for all the help, good to see Quzah is also still learning