The problem I'm having is this:

I have a table [MAX_ELEMENTS] [18] where the MAX elements can be as high as 100,000. The length of each element is 18 characters.

What I doing is simply loading in full 18 character records into this table.

The problem I'm having is that these fields are actually two fields and I'm trying to separate them where the first field is 12 characters and the second field is 6 (total of 18).

I tried "memcpy" for loops and nothing seems to work except that the single field can be copied into a single 18 character variable. But even after that, I can't separate them into two variables.

It appears that the only thing I can do is move the 18 cell from the table to the 18 variable and write the 18 character field into an output (as one field) instead of two (where I'm unable to manipulate or reformat these actual two values.

I'm new to C/C++, so I appreciate any help on this.

Thanks,

You may like this trick. :) If you treat your records as strings then you can easily place them in a single array and work with them using standard string operations. The cost is two extra characters per element.

#include <stdio.h>
#include <string.h>

#define MAX_ELEMENTS 2

char table[MAX_ELEMENTS][20];

void insert_record ( const char *record, int row )
{
  memcpy ( table[row], record, 12 );
  memcpy ( table[row] + 13, record + 12, 6 );
}

void print_record ( int row )
{
  printf ( "%s\n", table[row] );
  printf ( "%s\n", table[row] + 13 );
}

int main ( void )
{
  insert_record ( "abcdefghijkl123456", 0 );
  insert_record ( "lkjihgfedcba654321", 1 );
  print_record ( 0 );
  print_record ( 1 );
}

If the cost is too much for you then you can use fixed widths and forget about strings with only minor changes:

#include <stdio.h>
#include <string.h>

#define MAX_ELEMENTS 2

char table[MAX_ELEMENTS][18];

void insert_record ( const char *record, int row )
{
  memcpy ( table[row], record, 12 );
  memcpy ( table[row] + 12, record + 12, 6 );
}

void print_record ( int row )
{
  printf ( "%.*s\n", 12, table[row] );
  printf ( "%.*s\n", 6, table[row] + 12 );
}

int main ( void )
{
  insert_record ( "abcdefghijkl123456", 0 );
  insert_record ( "lkjihgfedcba654321", 1 );
  print_record ( 0 );
  print_record ( 1 );
}

Hi,

I like your answer but I'm still a little confused. After I load them into the table, I search for the valid record (via the first 12 characters - that part works fine). The next step is to memcpy them back into variables.

I then use fprintf to write the record out to a file after they are moved into the new variables. The possibility is that we may only use one ov the variables.

I appreciate your help.

Thanks,


Bob

Also, I'm using C++ which also has all the original "C" libraries available (can compile a "C" program with the C++ compiler).

>The next step is to memcpy them back into variables.
It's the same thing with different source and destination arrays:

char field1[13] = {0};
char field2[7] = {0};

memcpy ( field1, table[i], 12 );
memcpy ( field2, table[i] + 12, 6 );

Hi,

From what I understand from your example, you are actually splitting up the one record 18 character field as the records are being loaded into the table, correct?

With that being done, they can be retrieved into the two separate fields later in the program.

Why can't it just be separated as I was doing it. Also, you mentioned strings. What fields are initialized or treated as "strings"?

>Why can't it just be separated as I was doing it.
It can. Depending on the needs of the application, the choice is yours about when to do what as long as everything works as it should.

>What fields are initialized or treated as "strings"?
If a sequence of characters is terminated with the '\0' character, it's a string.

Narue,

I got it working with the Printf as you have it with the period and asterisk after the %.

Now I'm working on writing them into a file (using fprint).

I really appreciate your help on this.

Thanks,


Bob

This question has already been answered. Start a new discussion instead.