tags:

views:

690

answers:

4

i have a char array in a C app that i have to split into parts of 250 so i can send it along to another application that doesn't accept more at one time, how would i do that ? Platform: win32. Thanks in advance!

+4  A: 

From the MSDN documentation:

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

Note that strncpy doesn't check for valid destination space; that is left to the programmer. Prototype:

char *strncpy( char *strDest, const char *strSource, size_t count );

Extended example:

void send250(char *inMsg, int msgLen)
{
    char block[250];
    while (msgLen > 0)
    {
         int len = (msgLen>250) ? 250 : msgLen;
         strncpy(block, inMsg, 250);

         // send block to other entity

         msgLen -= len;
         inMsg  += len;
    }
}
jvasak
duh! how did i miss that?! thanks!
SinisterDex
That only copies the first 'count' characters from the array. How will you get the next 250?
Bill the Lizard
@Bill the Lizard: Every time he calls send250() and gets the return value he could call(off the top of my head, don't know the name) the method to split the array at that point. The only problem I see here is youre sending 250 characters each time, for example even if there's only 10 valuable ones.
junkforce
+1  A: 

I can think of something along the lines of the following:

char *somehugearray;
char chunk[251] ={0};
int k;
int l;
for(l=0;;){
  for(k=0; k<250 && somehugearray[l]!=0; k++){
    chunk[k] = somehugearray[l];
    l++;
  }
  chunk[k] = '\0';
  dohandoff(chunk);
}
warren
This doesn't null-terminate the last chunk correctly if it's shorter than 250 chars.
Roddy
+1  A: 

If you strive for performance and you're allowed to touch the string a bit (i.e. the buffer is not const, no thread safety issues etc.), you could momentarily null-terminate the string at intervals of 250 characters and send it in chunks, directly from the original string:

char *str_end = str + strlen(str);
char *chunk_start = str;

while (true) {
    char *chunk_end = chunk_start + 250;

    if (chunk_end >= str_end) {
        transmit(chunk_start);
        break;
    }

    char hijacked = *chunk_end;
    *chunk_end = '\0';
    transmit(chunk_start);
    *chunk_end = hijacked;

    chunk_start = chunk_end;
}
Ates Goral
A: 

jvasaks's answer is basically correct, except that he hasn't null terminated 'block'. The code should be this:

void send250(char *inMsg, int msgLen)
{
    char block[250];
    while (msgLen > 0)
    {
         int len = (msgLen>249) ? 249 : msgLen;
         strncpy(block, inMsg, 249);
         block[249] = 0;

         // send block to other entity

         msgLen -= len;
         inMsg  += len;
    }

}

So, now the block is 250 characters including the terminating null. strncpy will null terminate the last block if there are less than 249 characters remaining.

Mike Thompson
It's not stated in the question whether the char array is being used as a string or simply a small block of 8-bit data, but yes, you must null-terminate as you've shown above if string functionality is desired.
jvasak
It must be a string. If it is just a block of memory then strncpy will stop as soon as it finds a null. Binary data should use memcpy, not strncpy
Mike Thompson