Kevin Goodsell <usenet1.spamfree.fusion@neverbox.com> wrote in message news:<C2lcb.1843$NX3.421@newsread3.news.pas.earthl ink.net>...[color=blue]
> Keith Dewell wrote:
>[color=green]
> > 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.c ount,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);[/color]
>
> What is 'filename'? If it's a std::string, this is very bad (use
> filename.c_str() instead). If it's a char* or char[] this is
> unnecessary. The only case I can think of where you'd want to do this is
> if filename just happened to be a null-terminated vector<char>, which is
> unusual.
>[/color]
'filename' is an instance of FXString from the FOX Toolkit
(
http://www.fox-toolkit.org/); in our shop we use FOX instead of MFC
or whatever for GUI development. We use a dialog box for the user to
give the filename and this code uses FOX. I agree that '&filename[0]'
is ugly, but it is the only way I have found, so far, that works.
FXString does not understand c_str(). Anyway, this doesn't pertain to
my problem since this code successfully attaches the stream to the
file. But thanks for your input![color=blue][color=green]
> >
> > while(std::getline(inputFileStream[0], aString, '\t'))
> > dataFromFile.push_back(aString);[/color]
>
> It's hard to say what this will do without knowing something about the
> format of the file. Those char[] members you wrote to the file, might
> they have tab characters in them?
>[/color]
The values which the char[] members hold onto are just words like,
"The quick brown fox jumped over the lazy dog" -- or whatever words
may be found in a government project. :-) The only whitespace
permissable is a blank. Between the struct members I insert a tab
delimiter for later parsing (see MY_MACRO_FORMAT above).[color=blue][color=green]
> >
> > 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).[/color]
>
> Your problem does not appear to have anything to do with "mixing C and
> C++."
>[/color]
The reason I thought it may have to do with the mixing of languages is
that I write to the file with C and read the file with C++ where:
1)The only problem-child is the thirdMember, the first and second
members never take in garbage values and the only difference is the
size of the arrays in their definition, i.e., char firstMember[16] and
char secondMember[16] versus char thirdMember[96]. AND ...
2)The problem seems to have something to do with the "magical"
number 16, because if the thirdMember has < 16 characters then it
takes no garbage from the stream and the values look good.
[color=blue]
> There is nothing obviously wrong here (other than the
> '&filename[0]' thing). Try posting a complete, minimal program that
> demonstrates the problem, as well as some input to test it with.
>
> -Kevin[/color]
I started rewriting a minimal program but found it more involved than
expected; partly because of the presence of FOX, partly because I need
to change names and data for security reasons, and partly because of
the overarching functionality which crosses over a few different
classes. I haven't given-up on this yet and it may be a good approach
for troubleshooting anyway. But I thought I should go ahead and
respond to your post with the hope of presenting my problem more
clearly.
Thanks for all of your help,
Keith