Use strncpy instead of strcpy . That's all there is to it. (Caution: strncpy does not nul-terminate the destination string if it hits its limit.
).
Up vote 4 down vote favorite share g+ share fb share tw.
Char copy, array20 printf("enter ..."): scanf("%s", array); if (strlen(array) > 20 ) { strcpy(copy, array....); what would I need to do to make it only grab the first 20 character if the input is more then 20 character long c strcpy link|improve this question asked Feb 25 '11 at 21:28Thao Nguyen1527 84% accept rate.
1 Use strncpy instead. – James Feb 25 '11 at 21:32 1 What is that supposed to mean? Copy is not an array at all.
Array is 20 characters long. It array simply cannot be longer that 20 characters. – AndreyT Feb 25 '11 at 21:32 you overflow with scanf if you enter more than 20bytes.
Look at fgets. – Nyx0uf Feb 25 '11 at 21:35 @James strncpy doesn't necessarily NUL-terminate the result -- it's almost never what one wants (its original purpose was to copy UNIX directory entries, which fit in 14 bytes and were NUL-terminated if shorter). In any case, it isn't applicable here.
– Jim Balter Feb 25 '11 at 9:12.
Use strncpy instead of strcpy. That's all there is to it. (Caution: strncpy does not nul-terminate the destination string if it hits its limit.
) EDIT: I didn't read your program carefully enough. You lose already at the scanf call if user input is longer than 20 characters. You should be calling fgets instead.
(Personally I think *scanf should never be used - this is only the tip of the iceberg as far as problems they cause. ) Furthermore, copy has room for only one character, not twenty; but I'm going to assume that's a typo on your part.
Char array20+1; scanf("%20s", array); Problem solved.
1 for solving the problem at source, although I would prefer fgets. – Zack Feb 25 '11 at 21:39 1 @Zach – fgets would retain '\n'. – aaz Feb 25 '11 at 21:44 @Zach @aaz Except for in the case where the array was greater than 20, which means you'd also have to add logic to see whether or not it did retain the '\n' and replace as appropriate.
But on the flip side of the coin, with scanf you have to specify the size of the buffer twice (you can't pass an integral type to the scanf specifier for the width except with platform-specific extensions, so you have to specify it once as an integer when declaring the array and again in a string for the scanf specifier or do something ugly like snprintf(spec, sizeof(spec), "%d%%s", sizeof(buf));) – user470379 Feb 26 '11 at 3:07 @user470379 – You could use a preprocessor macro for the buffer size and paste it into the format string, that would be reasonably neat. But, regardless, they are conceptually not interchangeable: fgets gives physical lines (and you would typically want to malloc a larger buffer on failure), scanf("%s") gives logical names (trimmed, no internal whitespace, probably limited in length by semantics). – aaz Feb 26 '11 at 3:26.
Your question is not clear, since the code makes little or no sense. Your input cannot be longer than 20 characters since the receiving array is only 20 characters. If the user inputs more, your program will produce undefined behavior.
So, the main problem here is not limiting the copy, but rather limiting the input. However, your question seems to be about limited-length string copying. If that's what you need, then unfortunately there no dedicated function in standard library for that purpose.
Many implementation provide the non-standard strlcpy function that does exactly that. So, either check if your implementation provides strlcpy or implement your own strlcpy yourself. In many cases you might see advices to use strncpy in such cases.
While it is possible to beat strncpy into working for this purpose, in reality strncpy is not intended to be used that way. Using strncpy as a limited-length string copying function is always an error. Avoid it.
– Zack Feb 25 '11 at 21:37 @Zack: You can read about it in Wikipedia or here: stackoverflow.com/questions/2884874/… . Basically, strncpy is not a zero-terminated string function at all and its name is just a misnomer. It is just a historical blunder that lives on in the standard library.
– AndreyT Feb 25 '11 at 21:39 I would appreciate a pointer to an actual primary source for your assertion that strncpy was "created to fill 14-character long file name fields in one archaic version of Unix file system. " I don't see it in either of the places you point to. – Zack Feb 25 '11 at 21:44 Addendum: your assertion is backed up by C99 Rationale §7.21.2.4, but that's not a primary source either, and the C committee has been known to get things horrifically wrong.
– Zack Feb 25 '11 at 21:47 @Zack: Well, for a programmer of my generation, this is like trying to remember the first fairy-tale I read. Let's just say I heard about it Mr. Ritchie himself in one of his interviews (I do remember that he did talk about it), although there were many other sources. It is just something to memorize: strncpy was originally introduced for filling out 14-char file name fields in some old Unix filesystem.
– AndreyT Feb 25 '11 at 21:50.
Alternatively, you don't need to use strcpy to read just 20 characters (and you won't have to include strings. H): char c; for( I = 0; I.
Strncpy (copy, array, 20); does the trick. Exxcept the string would NOT be null-terminated if it was >20 chars! cplusplus.com/reference/clibrary/cstring....
Use strncpy. Make sure to null terminate the destination.
You need to change your scanf() call, not your strcpy() call: char copy20, array20; printf("enter...."); scanf(%20s",array); // read a maximum of 20 characters strcpy(copy, array).
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.