| | |
Loading and accessing 2 Dimensional tables
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2005
Posts: 27
Reputation:
Solved Threads: 0
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,
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.
If the cost is too much for you then you can use fixed widths and forget about strings with only minor changes:
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)
#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 ); }
C++ Syntax (Toggle Plain Text)
#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 ); }
I'm here to prove you wrong.
•
•
Join Date: Apr 2005
Posts: 27
Reputation:
Solved Threads: 0
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).
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:
It's the same thing with different source and destination arrays:
C++ Syntax (Toggle Plain Text)
char field1[13] = {0}; char field2[7] = {0}; memcpy ( field1, table[i], 12 ); memcpy ( field2, table[i] + 12, 6 );
I'm here to prove you wrong.
•
•
Join Date: Apr 2005
Posts: 27
Reputation:
Solved Threads: 0
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"?
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.
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.
![]() |
Similar Threads
- Long Island Guide (Website Reviews)
Other Threads in the C++ Forum
- Previous Thread: gcd problem
- Next Thread: Hecadecimal
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






