Adding A NULL Terminator To A String??

This topic is 4321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm trying to parse out some text into a buffer. I don't know the length of the string ahead of time. Hence, I declare a char array with size 256, which is more than needed. I copy the string of instrest with strncpy(MyBuffer, FoundString, FoundStringLength). However, the resulting buffer always has a massive size when I call strlen. I tried MyBuffer[(FoundStringLength + 1)] = 0 which gets me the true string length plus one when I call strlen. It is really not too big of a problem, but I'm curious what's going on. Is there a correct way to add a NULL terminator? Thanks in advance.

Share on other sites
I believe that the null character is actually '\0' not just '0'. Try that out.

Oh, and if possible, do yourself a favor and use std::string.

Thanks.

Share on other sites
You can just call strncpy with FoundStringLength + 1 as length, it will then copy the terminating NULL if there is one.

No matter what, always set the last character of the array to 0 after writing data to it to avoid string functions running off the end of it af you're ever missing a terminating null. Always use the buffer size (or size-1, this way you can zero the array once and always have a terminating null) as length limit for strncpy. This way, you will not have strncpy overwrite memory after the buffer array.

So, the call would look like this:
#define BUFLEN 255;char buffer[BUFLEN+1];memset( buffer, 0, BUFLEN+1 );/* get a string to found_string */strncpy( buffer, found_string, BUFLEN );/* you now have _at most_ the first BUFLEN characters of found_string  plus a terminating null character in buffer */

And yes, std::string rocks the big one [smile]

Share on other sites
Quote:
 Original post by EzbezI believe that the null character is actually '\0' not just '0'. Try that out.

The null character equals 0 anyway, so you can just as well type someChar = 0.

Is 'FoundStringLength' the number of characters in your char array? If so, MyBuffer[FoundStringLength] should be the position of the terminating character (your string goes from MyBuffer[0] to, and including, MyBuffer[Length-1]).

Anyway, use std::string. It will rid you of all these headaches, and is just as fast as any bit-twidling code you would type yourself. If used correctly, of course.

Share on other sites
Quote:
 Original post by fathom88I tried MyBuffer[(FoundStringLength + 1)] = 0 which gets me the true string length plus one when I call strlen. Is there a correct way to add a NULL terminator? Thanks in advance.

MyBuffer[FoundStringLength] = 0;

Remember that your call to strncpy fills MyBuffer[0] to MyBuffer[FoundStringLength - 1].

Share on other sites
Quote:
 Original post by fathom88I'm trying to parse out some text into a buffer. I don't know the length of the string ahead of time. Hence, I declare a char array with size 256, which is more than needed.

Is it really? You can pretty much always count on someone coming along and breaking something like that later. By the time you're really sure you have "enough" of an allocation, you are wasting huge amounts of space 99% of the time.

Not to mention, it's ugly. In C++ use std::string. In C... ugh, why use C any more?

But for the sake of understanding the technical aspects...

Quote:
 I copy the string of instrest with strncpy(MyBuffer, FoundString, FoundStringLength). However, the resulting buffer always has a massive size when I call strlen.

Right, because strcpy/strncpy don't add a null terminator. Of course, there is one right after the FoundString (assuming you didn't mess anything up), so you could just copy one more character.

Quote:
 I tried MyBuffer[(FoundStringLength + 1)] = 0 which gets me the true string length plus one when I call strlen. It is really not too big of a problem, but I'm curious what's going on. Is there a correct way to add a NULL terminator? Thanks in advance.

Well, did you try seeing what characters you have? Did you try putting the terminator one character earlier, seeing as you seem to get one character too many? This actually turns out to be the correct solution. Hint: If FoundStringLength == 0, where would you put the terminator?

But seriously, use a real string and save yourself the headaches.

• 10
• 16
• 9
• 13
• 41