Fgets doesn't work after scanf?

Scanf() reads exactly what you asked it to, leaving the following n from the end of that line in the buffer where fgets() will read it. Either do something to consume the newline, or (my preferred solution) fgets() and then sscanf() from that string.

Scanf() reads exactly what you asked it to, leaving the following \n from the end of that line in the buffer where fgets() will read it. Either do something to consume the newline, or (my preferred solution) fgets() and then sscanf() from that string.

Thanks. – Vayn May 6 at 23:59 Use something like %*^\n%*c at the end of the format to skip any characters up the newline, followed by the newline itself. – geekosaur May 7 at 0:07 I don't know this grammar before, thank you so much!

– Vayn May 7 at 0:11.

Scanf leaves whitespace in the input buffer, including new-line characters. To use fgets to read the next line you need to manually remove the rest of the current line: do{ int c = getchar(); }while(c! = EOF && c!

= '\n').

Yeah, that'll work too, but I personally just use fgets and then sscanf (as suggested by geekosaur) because it works "uniformly" for all sorts of data... not just single chars. And I like "generic, multi-purpose solutions". Cheers.Keith.

– corlettk May 7 at 0:32 This does work for more then a character though - that's what the loop is for ;) – missingno May 7 at 1:22.

Vayn, Geekoaur has answered your question well, I'm just pointing out another "problem" with your code. The line s1strlen(s1) = '\0'; is a no-op if s1 is allready properly null terminated BEFORE it executes. But if s1 is NOT allready properly null terminated BEFORE this line executes (and you're unlucky) it will cause: a SIGSEGV on a POSIX (*nix) system.

A GPF on Windows. This is because strlen basicaly finds the index of the existing null-terminator and returns it! Here's a valid, unoptimized implementation of strlen: int strlen(char *string) { int I = 0; while(stringi!

= '\0') { ++i; } return i; } So... If you're REALLY worried about strings NOT being null-terminated then you'd do something like: stringsizeof(string)='\0'; on local automatic strings (where the compiler "knows" the size of the string); or stringSIZE_OF_STRING for all other strings, where SIZE_OF_STRING is (most commonly) a #define'd constant, or a variable which you maintain specifically to store the current-SIZE (not length) of a dynamically allocated string. And if you're REALLY, REALLY, REALLY worried about strings not being null-terminated (like you're dealing with "dirty" libary methods (like Tuxedo's ATMI for example) you ALSO "clear" your "return strings" before passing them to the suspect library methods with: before:memset(string, NULL, SIZE_OF_STRING); invoke: DirtyFunction(/*out*/string); after: stringSIZE_OF_STRING='\0' SIG11's are a complete biatch to find because (unless you "hook" them with a signal-processor and say otherwise, they cause unix to hard-terminate your program, so you can't log anything (after the fact) to help figure out where-in-the-hell-did-that-come-from... especially considering that in many cases the line of code which throws the SIG11 is no-where-near the actual cause of the string loosing it's null-terminator. Does that make sense to you?

Cheers mate. Keith. PS: WARNING: strncpy does NOT allways nullterminate... you probably meant strlcpy instead.

I learned this the hard way... when a 60 million dollar billing run crashed. EDIT: FYI: Here's a "safe" (unoptimized) version of strlen which I'll call strnlen (I reckon this should be in stdlib. Sigh.).

// retuns the length of the string (capped at size-1) int strnlen(char *string, int size) { int I = 0; while( i.

I am novice at C, and the textbook doesn't talk about string and security much. Maybe my textbook isn't good enough :( Thank you for your professional advice :) – Vayn May 7 at 1:09.

Geekoaur has answered your question well, I'm just pointing out another "problem" with your code. The line s1strlen(s1) = '\0'; is a no-op if s1 is allready properly null terminated BEFORE it executes. A SIGSEGV on a POSIX (*nix) system.

A GPF on Windows. This is because strlen basicaly finds the index of the existing null-terminator and returns it! Or stringSIZE_OF_STRING for all other strings, where SIZE_OF_STRING is (most commonly) a #define'd constant, or a variable which you maintain specifically to store the current-SIZE (not length) of a dynamically allocated string.

SIG11's are a complete biatch to find because (unless you "hook" them with a signal-processor and say otherwise, they cause unix to hard-terminate your program, so you can't log anything (after the fact) to help figure out where-in-the-hell-did-that-come-from... especially considering that in many cases the line of code which throws the SIG11 is no-where-near the actual cause of the string loosing it's null-terminator. Does that make sense to you? PS: WARNING: strncpy does NOT allways nullterminate... you probably meant strlcpy instead.

I learned this the hard way... when a 60 million dollar billing run crashed. FYI: Here's a "safe" (unoptimized) version of strlen which I'll call strnlen (I reckon this should be in stdlib.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions