hi all....i have small problem with replacement of part of a character array.....

my problem is i have character buffer

buff[]

the content in it is


"DESCRIBE rtsp://<ip-addr>:9000/2.sdp RTSP/1.0

CSeq: 1


Bandwidth: 384000

Accept-Language: en-US

User-Agent:Windows NT 5.1Service Pack 2)"

now here i want to replace "9000" with the number i wish....how can i do it...

please help me out.....
thank you....

Recommended Answers

All 5 Replies

I would search for the second instance of the colon,

int pos;
bool found = false;
for(over the buffer)
        if (found && buff[i] == ':' )
        {
                 pos = i;
                 break;
         }
        else if(buff[i] ==':')
               found = true;
         etc.

march up until the / to get the digits length (or if you already know it skip this step) make sure the value you are inserting is the same number of digits and just write them into the array directly.

I would search for the second instance of the colon,

int pos;
bool found = false;
for(over the buffer)
        if (found && buff[i] == ':' )
        {
                 pos = i;
                 break;
         }
        else if(buff[i] ==':')
               found = true;
         etc.

march up until the / to get the digits length (or if you already know it skip this step) make sure the value you are inserting is the same number of digits and just write them into the array directly.

thank you for your suggestion.....but the string i want to inssert is either smaller than the present or it can be greater...... and kindly can you explain the code you mentioned above.....

I tossed in a bit of pseudocode there to make sure I didn't have all the fun. I'm sure there are more compact and elegant ways of doing it, but basically you march through the array looking for the first colon. When it's found you put up a flag saying ok I've found one colon. Keep marching until another colon is found. If it's the second colon, break out of the for loop and record the index.

Now, from what you are saying, the best thing to do would be to read the string up to the second colon like I said earlier, copy everything up to and including that position into a new buffer that is bigger than the first by the number of digits you want to insert.
Copy over your new digits, and finally copy from the / to the end of the buffer into the new one. You can definitely use strcpy, strncpy, etc if you want to also.

I tossed in a bit of pseudocode there to make sure I didn't have all the fun. I'm sure there are more compact and elegant ways of doing it, but basically you march through the array looking for the first colon. When it's found you put up a flag saying ok I've found one colon. Keep marching until another colon is found. If it's the second colon, break out of the for loop and record the index.

Now, from what you are saying, the best thing to do would be to read the string up to the second colon like I said earlier, copy everything up to and including that position into a new buffer that is bigger than the first by the number of digits you want to insert.
Copy over your new digits, and finally copy from the / to the end of the buffer into the new one. You can definitely use strcpy, strncpy, etc if you want to also.

Thats nice of you....thank you buddy

if you are using c++
there are useful containers in the stl standard template library

if you read up about strings you will find that a lot of the work has been done for you but beware replace() doesn't do what you would expect

//pseudo code to put buffer in string

#include <string>
std::string str;
//copy buffer into 
str.assign(pointer_to_buffer, buffer_size);

once you have a string there are functions that you can call to do
the common things you want find(), replace()

you need to find a method for finding your location

and something that looks messy is needed but is actually simple

like an int there is a type called size_type this is just an int without negative values.
so
std::string::size_type pos(0);
is just a number pointing to 0
to check if a string has been found

std::string to_find("9000"); 
pos = find(to_find, pos);
if(pos != std::string npos)
{
//we have found it starting at: pos
//so now you need to find the end and store it
}

.size() tells you howmany chars are in the string
so
to_find.size() //will return 4
so you can find the end of the string

finally replace will let the 9000 be changed to what ever you like
and in the long run this is how you will learn to do this and there are fewer errors to be made.


hi all....i have small problem with replacement of part of a character array.....

my problem is i have character buffer

buff[]

the content in it is


"DESCRIBE rtsp://<ip-addr>:9000/2.sdp RTSP/1.0

CSeq: 1


Bandwidth: 384000

Accept-Language: en-US

User-Agent:Windows NT 5.1Service Pack 2)"

now here i want to replace "9000" with the number i wish....how can i do it...

please help me out.....
thank you....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.