K
Keith Dewell
Greetings!
My current job has brought me back to working in C++ which I haven't
used since school days. The solution to my problem may be trivial but
I have struggled with it for the last two days and would appreciate
this group's helpful expertise.
My problem may be related to mixing the C and C++ languages together.
Namely, I have a struct which I cannot change (as legacy code)
similiar to this (I will change the names throughout as I am working
on a government project):
typedef struct {
char firstMember[16];
char secondMember[16];
char thirdMember[96];
unsigned long count;
} my_Struct_T;
Instances of this struct are written to a file using fprintf like
this:
if (file_p) fprintf(file_p, MY_MACRO_FORMAT);
where MY_MACRO_FORMAT is defined like this:
#define MY_MACRO_FORMAT \
"%-12.12s\t%-8.8s\t%6d\t%-56.56s\t", \
aStruct.firstMember,aStruct.secondMember,aStruct.count,Struct.thirdMember
(aStruct being an instance of my_Struct_T)
The user selects the written file using a dialog box from the GUI in
order to read-in this data. To do this I use C++ code as follows:
std::vector<std::string> dataFromFile;
std::string aString;
//actually passed-in from another method
std::ifstream inputFileStream(&filename[0], std::ios::in);
while(std::getline(inputFileStream[0], aString, '\t'))
dataFromFile.push_back(aString);
Now the problem ...
Stepping through the code in the debugger as values are written to the
vector, I see that the values for aStruct.thirdMember are always
garbage unless it contains 15 or less characters.
BTW, the values always look good in the file to which I have written,
regardless of the number of characters for thirdMember. (I confirm
this with a text editor.)
Please offer a solution where I can still take a C++ approach (instead
of C) as that is my preference, while not changing the legacy code
(namely, the struct above).
Much Thanks,
Keith
My current job has brought me back to working in C++ which I haven't
used since school days. The solution to my problem may be trivial but
I have struggled with it for the last two days and would appreciate
this group's helpful expertise.
My problem may be related to mixing the C and C++ languages together.
Namely, I have a struct which I cannot change (as legacy code)
similiar to this (I will change the names throughout as I am working
on a government project):
typedef struct {
char firstMember[16];
char secondMember[16];
char thirdMember[96];
unsigned long count;
} my_Struct_T;
Instances of this struct are written to a file using fprintf like
this:
if (file_p) fprintf(file_p, MY_MACRO_FORMAT);
where MY_MACRO_FORMAT is defined like this:
#define MY_MACRO_FORMAT \
"%-12.12s\t%-8.8s\t%6d\t%-56.56s\t", \
aStruct.firstMember,aStruct.secondMember,aStruct.count,Struct.thirdMember
(aStruct being an instance of my_Struct_T)
The user selects the written file using a dialog box from the GUI in
order to read-in this data. To do this I use C++ code as follows:
std::vector<std::string> dataFromFile;
std::string aString;
//actually passed-in from another method
std::ifstream inputFileStream(&filename[0], std::ios::in);
while(std::getline(inputFileStream[0], aString, '\t'))
dataFromFile.push_back(aString);
Now the problem ...
Stepping through the code in the debugger as values are written to the
vector, I see that the values for aStruct.thirdMember are always
garbage unless it contains 15 or less characters.
BTW, the values always look good in the file to which I have written,
regardless of the number of characters for thirdMember. (I confirm
this with a text editor.)
Please offer a solution where I can still take a C++ approach (instead
of C) as that is my preference, while not changing the legacy code
(namely, the struct above).
Much Thanks,
Keith