Loading and accessing 2 Dimensional tables

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Loading and accessing 2 Dimensional tables

 
0
  #1
Apr 3rd, 2005
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,
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loading and accessing 2 Dimensional tables

 
0
  #2
Apr 3rd, 2005
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.
  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:
  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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Loading and accessing 2 Dimensional tables

 
0
  #3
Apr 3rd, 2005
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loading and accessing 2 Dimensional tables

 
0
  #4
Apr 3rd, 2005
>The next step is to memcpy them back into variables.
It's the same thing with different source and destination arrays:
  1. char field1[13] = {0};
  2. char field2[7] = {0};
  3.  
  4. memcpy ( field1, table[i], 12 );
  5. memcpy ( field2, table[i] + 12, 6 );
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Loading and accessing 2 Dimensional tables

 
0
  #5
Apr 3rd, 2005
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"?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loading and accessing 2 Dimensional tables

 
0
  #6
Apr 3rd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Loading and accessing 2 Dimensional tables

 
0
  #7
Apr 3rd, 2005
OK.

I'll try it out.

Thanks for your help

Bob
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Loading and accessing 2 Dimensional tables

 
0
  #8
Apr 3rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Loading and accessing 2 Dimensional tables

 
0
  #9
Apr 3rd, 2005
Narue,

I got the program working.

Again, thanks.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC