Hi C++ Pros,
I have a lecacy C program that has been converted to C++, but is really still C. The program reads column delimited data into structures. The structures are designed to receive the data EXACTLY as mapped for each data element. To read each data element from the structures, instructions like this are used:
memcpy(PolFxdFndOut.Extractiondate, Var_Dat.PolEffDate, (size_t)sizeof(PolFxdFndOut.Extractiondate));
I would like something like:
newcpy(PolFxdFndOut.Extractiondate, Var_Dat.PolEffDate);
where newcpy figures out how many characters to copy.
There are thousands of similar memcpy statements in the program. It is not feasable to completely redesign the program using C++ String classes.
Here is an example (using memset) of what I would like, but doesn't work as is:
#include <iostream>
using namespace std;
typedef struct foo {
char x1[5];
char x2[9];
char x3[3];
int x4;
} FOO;
void mySet(char * data, const char val) {
//*** This doesn't work RIGHT because sizeof operator
//*** returns the size of the char pointer, NOT the size of
//*** the data (foo1.x2) that the pointer is pointing to.
//*** Nuts!
short lenth = sizeof(data);
memset(data, val, lenth);
}
int main () {
FOO foo1;
memset(foo1.x2, '?', sizeof(foo1.x2));
mySet(foo1.x2, '7');
cout << "foo1.x2 = " << foo1.x2 << " bummer!\n";
return 0;
}
Any brilliant ideas will be most welcome.
Thank you,
Norm
I have a lecacy C program that has been converted to C++, but is really still C. The program reads column delimited data into structures. The structures are designed to receive the data EXACTLY as mapped for each data element. To read each data element from the structures, instructions like this are used:
memcpy(PolFxdFndOut.Extractiondate, Var_Dat.PolEffDate, (size_t)sizeof(PolFxdFndOut.Extractiondate));
I would like something like:
newcpy(PolFxdFndOut.Extractiondate, Var_Dat.PolEffDate);
where newcpy figures out how many characters to copy.
There are thousands of similar memcpy statements in the program. It is not feasable to completely redesign the program using C++ String classes.
Here is an example (using memset) of what I would like, but doesn't work as is:
#include <iostream>
using namespace std;
typedef struct foo {
char x1[5];
char x2[9];
char x3[3];
int x4;
} FOO;
void mySet(char * data, const char val) {
//*** This doesn't work RIGHT because sizeof operator
//*** returns the size of the char pointer, NOT the size of
//*** the data (foo1.x2) that the pointer is pointing to.
//*** Nuts!
short lenth = sizeof(data);
memset(data, val, lenth);
}
int main () {
FOO foo1;
memset(foo1.x2, '?', sizeof(foo1.x2));
mySet(foo1.x2, '7');
cout << "foo1.x2 = " << foo1.x2 << " bummer!\n";
return 0;
}
Any brilliant ideas will be most welcome.
Thank you,
Norm