Copy part of a string in C

It seems like it should be really simple, but for some reason I am not getting a job. I have a line called seq that looks like this:

ala ile val 

I want to take the first 3 characters and copy them to another line. I use the command:

 memcpy(fileName, seq, 3 * sizeof(char)); 

This should do fileName = "ala" , right? But for some reason I get fileName = "ala9" . I'm working on it now, just saying fileName[4] = '\0' , but wondered why I get this 9.

Note: After changing seq to

 ala ile val ser 

and restarting the same code, file_name becomes "alaK" . Not 9, but still erroneous.

+6
c string
source share
10 answers

C uses a null terminator to indicate the end of a line. memcpy does not know that you are copying strings (it just copies bytes), so it does not think to put one. The workaround you have is actually the right answer.

Edit: wolfPack88 has a good point. You really need to change the file name [3]. Also in the comments below are some great points about strncpy, which is also worth exploring.

+18
source share

sprintf is your friend for extracting characters from the middle of one line and putting them in a zero-terminated character buffer.

 sprintf(fileName, "%.3s", seq); 

or

 sprintf(fileName, "%.*s", 3, seq); 

or even

 snprintf(fileName, sizeof(fileName), "%.*s", len, seq); 

will give you what you want. Version * allows for variable lengths and snprintf is safer to prevent buffer overflows

+11
source share

You need to install

 fileName[3] = 0; 

Make sure the file_name has enough space for the end of the NUL byte string.

+5
source share

You must use filename[3]='\0'; . Why this is necessary: ​​because nothing else set the NUL terminator for the string, so you need to.

Edit: Of course, for real use, you are not using a constant, as shown above. Usually you use something like:

 char *substring(char *out, char const *in, size_t len) { memcpy(out, in, len); out[len] = '\0'; return out; } 

Note that you really had the right idea using memcpy . strncpy (for an obvious example) is not quite what you need to use for this (or almost any other) purpose. In the list of standard library functions that should be avoided, strncpy takes the second place in the list, only gets behind (although, in fairness, I must indicate that strtok is a close third).

Also note that (like most standard library functions) this does not attempt to check the parameters that you pass, for example, if you tell him to copy 99 characters from a string up to 10 characters long to the buffer, long characters, he will try to copy 99 characters anyway by creating undefined behavior).

Edit2: One option is to use sprintf .

+5
source share

If you want to use memcpy to copy strings, you must set the character '\ 0' manually after the last character of the string. If you do not want to handle '\ 0' manually, use strcpy or strncpy instead.

+5
source share

The C standard library does not have a special function for copying part of a string. The correct way to do this is to use memcpy (as you already did), and the explicit null output is to complete the result. You forgot to complete the result, so you see strange extra characters after the copied part of the line.

Note that memcpy will only work if you know the length of the original string in advance, i.e. you know that the copied part of the line lies entirely inside the original line. If it is likely that the copied part of the source contains a terminating null character (i.e., the Source line ends in the middle of the copied part), then you will either have to write your own function to copy, or use non-standard but widely accessible strlcpy .

Sometimes you may find code samples that try to use the strncpy function for this purpose. Although in some cases it may seem to “work,” it makes no sense to use strncpy , given that it is not intended to be used that way.

+4
source share

Lines in C are null-terminated, which means you need a null character at the end of the line. It seems you were lucky to have only the next character to have a zero character, so you only have one extra garbage character, you could also get thousands of garbage characters ...

+3
source share

The reason is that you are copying three character bytes from seq, but there is no trailing null-char. Thus, you are not working around the workaround, but the right decision.

C lines must have zero termination. If this is not the case, the "user" reads the lines until he can read further, which leads to undefined behavior.

Btw, why not use strncpy?

+2
source share

An unexpected character is an artifact that does not have a null termination fileName .

In this case, fileName should be a char buffer having a length of at least 4 (three for three ala characters and one for a terminating null character). To set a null character, you can use:

 fileName[3] = '\0'; 

after memcpy .

+2
source share

In addition to the null ending line,

 fileName[3] = '\0'; 

You may also consider using strncpy instead of memcpy . In addition, sizeof(char) should always be evaluated at 1, so it is redundant.

Good luck

+2
source share

All Articles