user2220844
user2220844

Reputation: 11

Concatenate chars from one array to another in C

I have a text file containing 26000 lines, or records. Each record contains 128 bytes of hex data.

I need to pluck 4 records at a time out put it into another data structure and send it out on a socket

Here is a code snippet

std::string array[26000];
char* char_ptr = NULL;
char data[512];

    for(int index = 0; index <  26000; index +=  4)
    {
                    // copy data
                    char_ptr = &(array[index]);
                    memcpy(&data, char_ptr, sizeof(data));

                    // send data on socket
    }

This only gives every 4th 128byte record out of the string array. How can I make this take 4 records, or 512 bytes of data from the string array and copy it into the char data[] variable

Thanks for your help

Upvotes: 0

Views: 119

Answers (4)

Raven
Raven

Reputation: 1520

Your loop counting variable is counting up by 4, but you're only grabbing one record. I think a better way would be to let the string object take care of memory allocation for you, then just grab the characters from the resulting string and send it out.

for(int i = 0; i <  26000; i +=  4)
{
    string four_recs = array[i] + array[i+1] + array[i+2] + array[i+3];

    // Extract characters
    char *byte_data = new char [four_recs.length()];
    for (int j = 0; j < four_recs.length()]; j++)
        byte_data[j] = four_recs[j];


    // Send it out
}

Upvotes: 0

david
david

Reputation: 1016

First of all, a std::string is not the same thing as a byte array.

Second, the easiest way to build those four-record blocks would probably be an inner loop.

    for(int index = 0; index <  26000; index +=  4)
    {
                    // copy data
                    for (int j=0; j<4; j++) {
                        assert(array[index+j].size() == 128);
                        memcpy((data)+(128*j), array[index+j]+data(), 128);
                    }

                    // send data on socket
    }

Last, as the other answers have pointed out, using C-style arrays is error-prone; it might be better to work with std::string or other STL types until later, like when you actually send the data on the socket.

Upvotes: 1

AlexStepanov
AlexStepanov

Reputation: 481

I'd recommend you to avoid mixing C and C++ code. Here is how it can be done:

#include <numeric>
#include <string>

    std::string array[26000];
    const int add = 4;
    for (int i = 0; i < 26000; i += add)
    {
        std::string data;
        data = std::accumulate(array + i, array + i + add, data);

        // send data on socket
    }

I'd even replaced the first array of 26000 elements on a std::vector, and replaced the constant 26000 on size() method call. Working with C-style arrays is error prone.

Upvotes: 0

donjuedo
donjuedo

Reputation: 2505

You have 26,000 std::string objects, each with its own internal data, not guaranteed to have internal buffers consecutive from one object to the next. Your solution would be to revise your loop object to make use of each object separately as you populate the data array.

Upvotes: 0

Related Questions