|
Programm3r wrote: I know memcpy will work but it will also wipe the data that is already in the buffer.
What do you mean by that ? It will only make a copy of what is in the orignal buffer without modifying it.
|
|
|
|
|
OK, consider the following:
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[]="Another string";
memcpy (str2,str1,strlen(str1)+1);
printf ("Contents of buffer2:",str2);
return 0;
}
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
What do you need to do? Do you need to concatenate buffers?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
I don't see what you mean because your first explanation was a bit confusing. Of course that memcpy will erase what is in str2 because you want to copy the contents of str1 into str2. Which behavior are you looking for ? Your str2 can't hold its previous value and the new value.
I'm very confused as to what you are trying to achieve here
|
|
|
|
|
Programm3r wrote: char str1[]="Sample string"; char str2[]="Another string"; memcpy (str2,str1,strlen(str1)+1);
don't you haven't heard about pointer arithmetic.
memcpy (str2 + strlen(str2), str1, strlen(str1)+1); provided destination buffer has enough space.
anyway for string concatenation you don't need this if its raw buffer.
memcpy (byDestBuffer + dwCntDataInDestBuffer,
bySrcBuffer, dwCntDataInSrcBuffer);
dwCntDataInDestBuffer += dwCntDataInSrcBuffer;
|
|
|
|
|
guess not ....
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
you said you want to concatenate buffers, with memcpy you said you have issues i don't see any issues whether its string or raw buffer and such a simpler question here.
|
|
|
|
|
What exactly do you need to do?
i.e. What is the input and what is the expected output?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
I'm receiving a minutiae buffer from a biometric reader, and there is alot of null charaters in the buffer that I received. I then want to concat it to another buffer. And that is aboput that ....
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
Programm3r wrote: I then want to concat it to another buffer. And that is aboput that ....
Ooookayy, you should have said that in your first message, it would have made everything a bit clearer . I suggest you use a std::string instead of plain char arrays, it is possible to store null characters and it is much easier to do string concatenation.
|
|
|
|
|
Cedric Moonen wrote: I suggest you use a std::string instead of plain char arrays,
Only if he was using C++.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Rajesh R Subramanian wrote: Only if he was using C++.
Errr.. yes. But as the title of the message board is Visual C++ and that he didn't specifically said he was working in C, I concluded he was using C++ (yeah I know, jumping a bit too fast on the conclusion )
|
|
|
|
|
Actually, I had been stupid enough to use things like cout in the solution I provided to him, even though I assumed it was C (just modified it to use printf ). We're addicted to C++, you see.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Rajesh R Subramanian wrote: We're addicted to C++
What are these two fancy symbols following the only Real Man's Language?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
CPallini wrote: What are these two fancy symbols following the only Real Man's Language?
Symbols of evolution.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Could you make a detailed example (of what you have and what do you want to obtain)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
memcpy should work. Anyways, I want you to get an idea of what, this problem is all about. If you know the length of the string, then something like this might work for you. This is just a rough idea, you might want to make it better and more secure:
#include "stdio.h"
void strcpyEx(char* dest, const char* source, int iLen)
{
int i;
for(i=0;i<iLen;++i)
{
dest[i] = source[i];
}
}
int main()
{
char a[] = {'\0','\0','H','i','\0'};
char b[5];
strcpy(b,a);
printf("Attempting strcpy... b[3] is %d\n",b[3]);
strcpyEx(b,a,4);
printf("Attempting strcpyEx... b[3] is %d\n",b[3]);
return 0;
}
|
|
|
|
|
Some formatting reviewal needed.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Fixed, thanks.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
|
Programm3r wrote: I know memcpy will work but it will also wipe the data that is already in the buffer.
One of the basic rules of physics states that no two objects can occupy the same space. Of course memcpy() will wipe out the destination, but so will any other function.
While concatenation is a completely different subject, memcpy() can still be used.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
DavidCrow wrote: One of the basic rules of physics states that no two objects can occupy the same space.
Perhaps Mr.Bose and Mr.Einstein would remark on the above.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
modified on Thursday, May 8, 2008 11:13 AM
|
|
|
|
|
I was stating the obvious, and I believe so are you.
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
DavidCrow wrote: One of the basic rules of physics states that no two objects can occupy the same space
I think you forgot to add: "at the same time"
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
|
|
|
|
|