944,099 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3898
  • C++ RSS
Apr 3rd, 2005
0

Loading and accessing 2 Dimensional tables

Expand Post »
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,
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

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.
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_ELEMENTS 2
  5.  
  6. char table[MAX_ELEMENTS][20];
  7.  
  8. void insert_record ( const char *record, int row )
  9. {
  10. memcpy ( table[row], record, 12 );
  11. memcpy ( table[row] + 13, record + 12, 6 );
  12. }
  13.  
  14. void print_record ( int row )
  15. {
  16. printf ( "%s\n", table[row] );
  17. printf ( "%s\n", table[row] + 13 );
  18. }
  19.  
  20. int main ( void )
  21. {
  22. insert_record ( "abcdefghijkl123456", 0 );
  23. insert_record ( "lkjihgfedcba654321", 1 );
  24. print_record ( 0 );
  25. print_record ( 1 );
  26. }
If the cost is too much for you then you can use fixed widths and forget about strings with only minor changes:
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_ELEMENTS 2
  5.  
  6. char table[MAX_ELEMENTS][18];
  7.  
  8. void insert_record ( const char *record, int row )
  9. {
  10. memcpy ( table[row], record, 12 );
  11. memcpy ( table[row] + 12, record + 12, 6 );
  12. }
  13.  
  14. void print_record ( int row )
  15. {
  16. printf ( "%.*s\n", 12, table[row] );
  17. printf ( "%.*s\n", 6, table[row] + 12 );
  18. }
  19.  
  20. int main ( void )
  21. {
  22. insert_record ( "abcdefghijkl123456", 0 );
  23. insert_record ( "lkjihgfedcba654321", 1 );
  24. print_record ( 0 );
  25. print_record ( 1 );
  26. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

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).
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

>The next step is to memcpy them back into variables.
It's the same thing with different source and destination arrays:
C++ Syntax (Toggle Plain Text)
  1. char field1[13] = {0};
  2. char field2[7] = {0};
  3.  
  4. memcpy ( field1, table[i], 12 );
  5. memcpy ( field2, table[i] + 12, 6 );
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

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"?
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

OK.

I'll try it out.

Thanks for your help

Bob
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

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
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
Apr 3rd, 2005
0

Re: Loading and accessing 2 Dimensional tables

Narue,

I got the program working.

Again, thanks.
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: gcd problem
Next Thread in C++ Forum Timeline: Hecadecimal





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC