|
I know I have been stuck on the same problem for the last few days but, I have to get this to work or my rpogram won't function correctly. Like I need it to.
I am using the getline() function to read in a file. I need to put the first word on each line in a char array.
<br />
string name, unit,min,max,value; <br />
<br />
for(int i=0; i<45; i++){<br />
getline(fin, name, ',');<br />
<br />
if(name == "STOP")<br />
break;<br />
getline(fin, unit, ',');<br />
getline(fin, min, ',');<br />
getline(fin, max, ',');<br />
getline(fin, value);<br />
<br />
cout<<endl<<name<<"\n";<br />
strcpy(TSimHeader_arr[0].Name, (char*)name.c_str());<br />
strcpy(TSimHeader_arr[0].Name, name); <br />
strcpy(TSimHeader_arr[0].Name[i],name.c_str());<br />
TSimSignal_arr[0].Value[i] = atof(value.c_str());<br />
<br />
<br />
cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name[i];<br />
<br />
<br />
sig_count ++;<br />
<br />
}
All of the above are ways that I have tried. I don't get the correct word put in the char array.
I know the words are getting read in from the file correctly.
They are just not getting put in the char[] correctly.
Is there another way that I could go about doing this?
Is this the best wat to read in a file and put the words in a char[]?
I have to use a char[] because of certain requirements that my boss has given me.
I would love to use a vector, but it won't work
Thanks for your help,
Steven
|
|
|
|
|
If I remember your structure correctly, the first strcpy() method will work (you don't need the cast, though):
strcpy(TSimHeader_arr[0].Name, name.c_str()); What won't work is the method you're using to print it out:
cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name[i]; You need to print out what you store to:
cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name; Your version was only printing one character, right?
Ryan "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"
|
|
|
|
|
You are right it was only printing ouot 1 character.
But, if later on I need to access the array of names and get Names[15] how can I do that?
I just tried to print out TSimHeader_arr[0].Names[15] and it printed out <null>
Are the words getting put in the char Names[]?
Sorry, but I just don't quite understand.
Thanks, Steven
|
|
|
|
|
At their core, strings are an array of characters. The string "Hello" has 5 characters (plus a terminating NULL character), so it will need a 6-character array.
Your Names array is declared as char Names[45] , which means that it holds 45 characters, ie one string that can be up to [edit]44[/edit] characters long. If you are wanting to store more than one string, this won't work - an array of char s can only store one string.
To store multiple strings you'll need to use a 2-dimensional array - an array of arrays of characters (think about it, it does make sense )
I think you're wanting to store 45 strings. If each string is at most 10 characters long, then you'll need to declare Names as
char Names[45][11]; This is an array that holds 45 arrays of 11 characters, ie. it holds 45 strings that are up to 10 characters long each (remember the terminating NULL?). You'll have to work out how big to make the arrays to fit your strings (change the 11 to whatever is necessary to fit the string in). To write to these strings, use the code
strcpy(TSimHeader_arr[0].Names[0], name.c_str());
strcpy(TSimHeader_arr[0].Names[1], name.c_str());
strcpy(TSimHeader_arr[0].Names[44], name.c_str()); To display the strings, use
cout << TSimHeader_arr[0].Names[0] << endl;
cout << TSimHeader_arr[0].Names[1] << endl;
Hope this helps,
Ryan "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,
That makes sense. Which would take up less space an array of
char Names[45][10] or string[45]?
I am trying to use the least amount of space.
Thanks,
Steven
|
|
|
|
|
Names will be 450 bytes. I'm not sure about string as I don't use them. What does sizeof() report?
|
|
|
|
|
johnstonsk wrote:
Which would take up less space an array of
char Names[45][10] or string[45]?
string[45] would take a lot more space because of how it allocates the strings but since the data set is soooooo small I would not worry about this at all.
Anyways it should be
char Names[45][11]
because you need 1 extra space for the '\0' character that terminates strings. Otherwise your strings would only be able to hold 9 characters...
John
|
|
|
|
|
The first one uses less memory.
Are you wanting to transmit the information across a serial port or network of something like that? If you are, you can't use the second one - string internally stores the text as a pointer, so if you transmit it, you just transmit the pointer, not the text it points to. In this case, you'll have to use the first option. Just remember to increase your array size to fit your string in
Ryan "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"
|
|
|
|
|
Yes, I have a PCI card with a gig of memory that is shared with 5 other machines.
Some of the words are longer than others so, if I just make the 2nd dimension the size of the longest word will I have any problems reading them on the other side of the network?
thanks,
Steven
|
|
|
|
|
why not just to read it and stick it into a CStringArray ?
You already have it as part of MFC..
CStringArray myarray;
myarray.Add (StringFromFile);
that's it.
|
|
|
|
|
One other thing this is not a CString. CString is a class in MFC and ATL/WTL to handle strings..
John
|
|
|
|
|
By using the commands 'RegCreateKeyEx', 'RegSetValueEx' and 'RegQueryValueEx' you have only access to your own PC.
I wont to read the registry values of an external PC which is on the same network linked.
Is there somebody who has an idea?
|
|
|
|
|
See RegConnectRegistry in the doc.
onwards and upwards...
|
|
|
|
|
Hi,
I'm developing a color calibration application under WindowsXP with Visual C++ and MFC. I would like to have a dialog box where the user can adjust the screen's overall contrast and brightness.
The first solution would be to control it with some SDK API calls, but I don't really know how I should do it. I've tried to get the desktop's dc and use the SetColorAdjustment( ) with it, but it doesn't really work.
Then I've read about the VESA DDC/Ci standard which makes it possible to actually control the monitor from the software but the Win32 API doesn't provide an interface for this. (If you know some info about it which might be helpful that would be the best).
I'd appreciate if someone could provide some info on these things.
Thanks in advance
Oliver
|
|
|
|
|
This is a driver-specific feature (not all drivers support it), and I don't know of any Windows built-in functions for doing it. I am under the impression that the driver provides this capability, not Windows. You may have to communicate directly with the video card driver.
oliver.hu wrote:
The first solution would be to control it with some SDK API calls, but I don't really know how I should do it. I've tried to get the desktop's dc and use the SetColorAdjustment( ) with it, but it doesn't really work.
This only works for what you are about to draw, not what's there already, and it only works for that particular device context - other programs and Windows itself will not see any changes.
Ryan "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 have an NVidia Quadro NVS in my machine, so I downloaded the NV SDK but it doesn't really say anything about contrast or brightness or ddc/ci, or anything like that.
So far I thought that communicating with the video driver takes only a .lib or a .dll and a .h file, but it doesn't seem to be the case. In fact I already got into the DDK but that's just seems too complicated, and actually I'm not so sure whether I have to go down so deep to the kernel mode...
I saw very simple applications which do the same, they can control overall brightness, contrast and rgb gamma, and therefore I don't think writing a driver is absolutely necessary.
I've searched through NVidia's site for some info on the above, but in fact I haven't yet found anything useful.
Oliver
|
|
|
|
|
You have to change the bitmap pixels before you display them..
John
|
|
|
|
|
hi
How do i create CEedit cotrol dynamicly, withour resoruce editor,
i have crated a class subclass of CWnd
how do i add a CEdit and CStatic (label) control to it, where do i create it in OnCreate function ????
thanks
~dzenan~
|
|
|
|
|
Your question is not very clear: you have derived a class from CEdit and you want to create dynamically on your window? If so, just call Create:
CMyEdit NewEdit;<br />
NewEdit.Create(...);
Hope this helps
|
|
|
|
|
sorry for not clearing it up,,
I have a Class wich is derived from CWnd, now i want to put a Edit control on that window. how do i do that, or do i have to first create a class wich is derived from CEdit
thanks
~dzenan~
|
|
|
|
|
No sorry, I misunderstood your question, it's end of the day here !
So, that's a good question. I had the same kind of problem some time ago. I solve this by sending a user defined message to the window just after it was created:
CYourWnd Window;<br />
... Some code;<br />
Window.ShowWindow(SW_SHOW);<br />
Window.SendMessage(WM_MYUPDATE);
Then you have to add manually a handler for this message. In this function, create dynamically the objects.
Don't know if there is an easiest way ??
|
|
|
|
|
I see, will work on it
thanks
~dzenan~
|
|
|
|
|
Yeah, why not just override the Create function from the CWnd class and put it there?
[EDIT: Whoops, Iain Clarke mentioned that very solution below. ]
"When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity." - Albert Einstein
|
|
|
|
|
hmm well you dont need to create a class derived from CEdit.
Just Create a CEdit object in the OnCreate Event check out the code below
void MainFrame ::OnCreate(LPCREATESTRUCT lpcs)
{
CRect rect;
rect.SetRect(10,10,100,30);
CEdit *myeditbox = new CEdit;
CEdit *myedit = new CEdit;
myedit->Create(WS_CHILD|WS_VISIBLE,rect,this,300);
}
regards
Ahmed Ajmal
|
|
|
|
|
Here goes...
In your header:
class CMyWnd : public CWnd
{
...
protected:
CStatic m_StaticSubWnd;
CEdit m_EditSubWnd;
....
};
In your implementation:
int CMyWnd::OnCreate (LPCREATESTRUCT lpC )
{
m_StaticSubWnd.Create ("text here", WS_CHILD | WS_VISIBLE | SS_...,
CRect (0,0, lpC->cx, 24), this, 1);
m_EditSubWnd.Create ( WS_CHILD | WS_VISIBLE | ES_...,
CRect (0,26, lpC->cx, lpC->cy - 26), this, 2);
return CWnd::OnCreate (lpCreateStruct);
}
The coordinates are just examples. You can give them any coords you like.
You should handle WM_SIZE / OnSize and change the position / size of the subwindows when the size
of CMyWnd changes.
OK?
Iain.
|
|
|
|
|