's in it
This is a discussion on concatenating a string with '
's in it within the C++ Programming
forums, part of the General Programming Boards category; how do you concatenate a string with 0 bytes ('
') in them? i tried to write a while loop but ...
concatenating a string with '\0's in it
how do you concatenate a string with 0 bytes ('\0') in them? i tried to write a while loop but it doesn't work either. i don't know maybe i just did something wrong. here is my code:
i set str1 to the char array i want to add to, str2 to the string i want to add, a to the amount of characters i want to add, and offset to the position in str1 i want to start adding str2 at.
void stradd(char str1, char str2, int a, int offset)
int i = 0;
while (i < a)
str1[offset + i] = str2[i];
is there anything wrong with this code or am i just somehow doing something wrong?
I don't know what your "wrong" is but I'm assuming that something is going wrong when you're trying to use the string in combination with other, standard functions like printf or the ones from string.h.
This would be because after the concatenation str1 is not ended with a '\0' so you'd have to put a str1[offset + i] = '\0'; after the loop.
i'm wondering if its because this function is in a dll and its being called by a program that expects a null terminated string i think. the only thing is the program calling it can add a 0 byte to the string and that will show... do you think thats the problem?
Still not very clear to me what result you're expecting.
Give me an example of str1, str2 and the result.
There is certainly a problem if offset+i exceeds the range of str2 or if i exceeds the range of str1.
I used to be an adventurer like you... then I took an arrow to the knee.
str1 would be "hey i'm going to the store" and str2 would be "\0\0\0 ok". i want to turn that into "hey i'm going to the store\0\0\0 ok".
when i'm doing it in my program though the array starts empty and i try to add an array that starts with a \0 immediatly.
Well, your function works as it should so the problem lies elsewhere.
and the hat of mystery
> how do you concatenate a string with 0 bytes ('\0') in them?
You don't, since they're no longer strings.
You now have to use the mem... functions rather than the str... functions, and keep a separate count of how many bytes you have stored.
> i want to turn that into "hey i'm going to the store\0\0\0 ok".
And then what?
Sure you can create it, but what are you going to do with it when you've got it. The "ok" is going to be invisible to every standard string function.
Popular pages Recent additions
By digioz in forum C# Programming
Last Post: 09-07-2008, 04:30 PM
By BKurosawa in forum C++ Programming
Last Post: 08-09-2007, 01:02 AM
By NANO in forum C++ Programming
Last Post: 12-09-2002, 02:23 PM
By JCK in forum C++ Programming
Last Post: 12-08-2002, 01:45 PM
By spentdome in forum C Programming
Last Post: 05-27-2002, 06:49 PM