3

I have got some data in a buffer and want to put those data in an array.

 typedef struct chunk 
 {
 char data[300];    /* the bufferr. */
 } CHUNK;
 char *buffer, CHUNK c [100];

Assuming I have got data into the buffer, how can I put 300 char per chunk? I'm new to C so please explain me with simple example.

Thanks, Kevin

3
  • You haven't specified the question adequately, so all the answers below are credible in some situation and flawed in another. Are you dealing with textual data (and if so, is it ASCIIZ?, do you want each chunk to be NUL terminated?), or binary data (in which case you need to know the size of the data in buffer). What you want done with any unused CHUNK data elements/space. Commented Apr 13, 2011 at 4:20
  • Yes, my data will be just text file. Until now no idea with the unused CHUNK data. How should I do ? Sorry I'm new to C. Commented Apr 13, 2011 at 5:50
  • C normally stores strings as a series of numbers from 0 to 255, with 0 indicating the end of the text, and other numbers representing letters according to the ASCII code (e.g. 32 is a space, 65 a capital 'A'). You need to work out if whatever you will later do with the chunks needs each full 300-character chunk to have its own 0 C-string terminator; ditto for any final partly-filled chunk. So, how will you use the chunks later on? Commented Apr 13, 2011 at 6:50

3 Answers 3

3

The declaration is invalid, but I think you mean:

typedef struct chunk 
 {
     char data[300];    /* the bufferr. */
 } CHUNK;

 char *buffer;
 CHUNK c [100];

If I understand your question correctly (which I'm far from certain that I do), the code would be something like:

 int j = 0;
 char *bp = buffer;
 while (*bp)
 {
     strncpy (c [j] .data, bp, 300);  // copy data into next item
     bp += strlen (bp);
     ++ j;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

thx . But It doesn't loop . It only loop for once. what does bp+=strlen(bp) do?
bp += strlen(bp) is the same as bp = bp + strlen(bp) which means advance the pointer bp by the length of the string. It would only loop once if buffer had less than 300 characters in it.
2

In C, you can copy memory from one area to another using memcpy(). The prototype for memcpy() is:

void *memcpy(void *dst, const void *src, size_t n);

and the description is that it copies n bytes from src to dst, and returns dst.

So, to copy 300 bytes from b to a where both a and b point to something useful, b has at least 300 bytes of data, and a points to at least 300 bytes of space you can write to, you would do:

memcpy(a, b, 300);

Now your task should be something along the lines of:

typedef struct chunk 
{
    char data[300];
} CHUNK;
char *buffer;
CHUNK c[100];
size_t i;

/* make buffer point to useful data, and then: */
for (i=0; i < 300; ++i)
    memcpy(c[i].data, buffer+i*300, 300);

1 Comment

one question . since i'm reading from the buffer I can't use the looping you mention since the size of buffer can vary . How should I do ???
1

You can use strncpy.

strncpy( data, buffer, 299 ) ;

Leaving the last index for the termination character '\0'. Or make the array size 301 and then use strncpy for 300 elements.

5 Comments

@kevin - The link given has a simple example and explanation that you can check out.
Hi when i copy the next 300 char, will it know where to start to copy?
@kevin - strncpy will always start from the starting of the index of the array by default. If needed to started from a different index, give data+i where i is the position to start from. So if previously copied with any values in data will get over written with new values.
@mahesh - thank a lot :D but one question " how should I loop since the length of the buffer can vary "!!
@kevin - I didn't understand what you asked. But to write the length of the buffer to data, toBeReadLength = strlen(buffer); while( toBeReadLength) { strncpy( data, buffer, toBeReadLength ); toBeReadLength = strlen(buffer); }. But this kind code has a red flag since there is no check for buffer overflows. It works fine as along as the toBeReadLength is less than 300.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.