|
I have been using the getline() to read in lines from a file and delimiting on commas.
I need to set the items in a struct to these values that I read in.
When I get the data from the struct, I get garbage out.
Am I doing this wrong?
Some of the variables that are read in from the file are doubles and I have been using the atof() function to convert from string to double. But, the only way that the struct will allow me to put them in is if I use the c_str() function.
Thanks,
sj
<br />
<br />
<br />
struct TSimSignal<br />
{<br />
double Min[5]; <br />
double Max[5]; <br />
double Value[5]; <br />
string Name[5];<br />
string Unit[5];<br />
};<br />
<br />
<br />
<br />
void setInitialValues(TSimSignal &s){<br />
<br />
string name,unit,min,max,value;<br />
<br />
fin.open("mockData.dat"); <br />
<br />
for(int i=0; i<sizeof(s.Name); i++){<br />
getline(fin, name, ',');<br />
if(name == "STOP")<br />
break;<br />
getline(fin, unit, ',');<br />
getline(fin, min, ',');<br />
getline(fin, max, ',');<br />
getline(fin, value);<br />
<br />
<br />
}<br />
<br />
<br />
}<br />
<br />
|
|
|
|
|
Just a hint. When you're posting code, replace the '<' and '>' with '<' and '>' respectively, otherwise things like your for loop won't turn out properly.
Your code looks correct. Is the data getting loaded correctly? Can you see the values in the strings with debugger before you store it to the struct? Is fin declared as an ifstream ? You might want to post the code with the '<' and '>' changed - it would make it a lot easier to see what it's doing
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
Sorry about that, slipped my mind.
After watching the debugger, I realized the problem lies when I read the data back out of the struct.
I was using the sizeof(s.Name) thinking that it would return 5, but it was returning 80.
OK..... The getline returns a string. I really need my struct to have chars instead of strings.
But as you know; you can't convert a sting to a char.
Is there any way to do this?
I've only been coding for a few months now.
Thanks for the patients,
sj
-----------Right now it is......----------
struct TSimSignal<br />
{<br />
double Min[5]; <br />
double Max[5]; <br />
double Value[5]; <br />
string Name[5];<br />
string Unit[5];<br />
};
-----I really need it to be.....----------
struct TSimSignal<br />
{<br />
double Min[5]; <br />
double Max[5]; <br />
double Value[5]; <br />
char Name[5];<br />
char Unit[5];<br />
};<br />
|
|
|
|
|
johnstonsk wrote:
char Name[5];
char Unit[5];
Actually, it should be:
char *Name[5];
char *Unit[5]; since C strings are a pointer to an array of characters. Then do this:
s.Name[i] = strdup(name.c_str());
s.Unit[i] = strdup(unit.c_str()); Hope this helps,
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
|
@Ryan : After using the "show me all" routine :
for(int i=0; i<sizeof(s.Name); i++){
getline(fin, name, ',');
if(name == "STOP")
break;
getline(fin, unit, ',');
getline(fin, min, ',');
getline(fin, max, ',');
getline(fin, value);
~RaGE();
|
|
|
|
|
Perhaps Chris should add a script that automatically converts '<' and '>' in <pre> tags to HTML character codes .
and possibly '&' as well
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
I wish he did that. I know you can say no html that looks ok but if you want to use the <pre> tags it is a pain to edit your code samples..
John
|
|
|
|
|
What is the format of mockData.dat?
|
|
|
|
|
pla,deg,0.0,130.0,0.0
pitch_stickF,lbs,-3.28,4.4,0.0
roll_stickF,lbs,-13.0,13.0,0.0
yaw_pedal,lbs,-3.87,3.87,0.0
spbrk_pos,retract.hold.extend,-1.0,1.0,0.0
STOP,1
This is just a test file. The actual file has around 500 lines.
sj
|
|
|
|
|
This is not the recommended way to do what you are doing, but I just wanted to show you an alternative.
struct TSimSignal
{
double Min;
double Max;
double Value;
char Name[32];
char Unit[32];
} t[500];
FILE *pFile;
for (int x = 0; ...)
{
sscanf(pFile, "%[^,],%[^,],%f,%f,%f", t[x].Name, t[x].Unit, &(t[x].Min), &(t[x].Max), &(t[x].Value));
}
|
|
|
|
|
Instead of putting "i<sizeof(s.Name)" in your for loop, use "i<sizeof(s.Name)/sizeof(string)". sizeof() gives you the size of the variable/type in bytes , not the number of elements in the array. The size of a string is at least 4 bytes, so your loop will go at least 20 times - 5*sizeof(string). Your memory will therefore be overwritten by any writes after the 5th loop, and you'll end up with rubbish.
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
Thanks Ryan it works great.
Is there anyway that I can read the file and the items read in are chars instead of strings?
You da' man,
Sj
|
|
|
|
|
johnstonsk wrote:
Is there anyway that I can read the file and the items read in are chars instead of strings?
There is, but I don't really think there's a lot of merit in doing so. Using the string functions is safe - it prevents buffer overflows by allocating just the right amount of memory. Trying to allocate memory yourself is just a PITA and can cause all sorts of security errors. I would stick with what you're doing. If it works safely, there's not much point changing it.
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
I want to paste a icon from resource to a button by using SetIcon(), but neither m_bttnOpen.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON_OPEN));
nor
m_bttnOpen.SetIcon(::LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_OPEN)));
can do this.
Why?
And how to do this by the most simple way?
thankx
|
|
|
|
|
olinn wrote:
Why?
Has your button the icon style checked ? (Go in the ressource editor, right click on your button, tab "styles", check "Icon").
olinn wrote:
And how to do this by the most simple way?
This one :
m_bttnOpen.SetIcon(::LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON_OPEN)));
~RaGE();
|
|
|
|
|
I know that ASCII numbers 27 and 28 represent up and down arrows. Is there a way to get these symbols into a CSTring?
thanks
|
|
|
|
|
CString cUpDown = "\x1B Up and down arrows \x1C";
onwards and upwards...
|
|
|
|
|
Thanks for your reply, but the resulting string just has dark black lines were I was hoping to see arrows.
|
|
|
|
|
That is dependent upon which font you are using to display the text. Look at the CharMap application. Some fonts have corresponding characters for ^ and v, some don't.
onwards and upwards...
|
|
|
|
|
|
Dear everyone,
I create some CEdit box, the height for example is 20.
and the CEdit box is not multi-line.
But the font size is small, for some function, should
set the text content verticle center or bottom in CEdit.
How can i do?
Welcome any suggestion!
Many thks to u!
Best Regards,
Bosicat
Good Luck
|
|
|
|
|
Sorry, you can't centre the text in an edit box. It can't be aligned with the bottom either. Top-only, I'm afraid
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|
|
Thomas George wrote:
Do anyone know of a way to set different background and foreground colors for the text on individual toolbar buttons?
WM_CTLCOLOR?
Ryan
Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"
|
|
|
|
|