|
This program needs to read through the vector and essentially reads off strings sequentially as it is passed through the functions. This is old code but there is too much to rewrite anew. So the first function reads several "lines" (i.e. strings) an then the vector is passed to another function which needs to read off a variable number of "lines" and passes to other functions, etc. Each read takes the next string, thus, I am hopint to pass the iterator to keep track where it is (each function using the iterator to know which index is next to read). I have not used iterators except in very basic uses within one fuction and have not figured out how to pass one. Thanks.
|
|
|
|
|
I don't see why it would be hard to pass one - where are you stuck ?
Iterators from a vector support random access, so you can both use ++ and -- to step.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
Here is what I have:
static int read_layout(std::vector<std::string> &, vector<std::string>iterator, char *)
static int read_layout(std::vector<std::string> & trs, vector<std::string>iterator pr, char *szName)
{
}
void StartReading(std::vector<std::string> & trs)
{
vector<std::string>::iterator pr;
pr = trs.begin();
string s = *pr;
strcpy( szHeader, s.c_str() );
pr++; //there are several of these types of access prior to function call
iStatus = read_layout_truss(trs, pr, szName)
}
I am not able to get this to compile as is. Hopefully I did not post this previously as my keyboard seems to be freezing on me. Thanks for your input.
|
|
|
|
|
I see - damn typedefs. They should not do any harm, beyond obsfucating your code.
I suspect you need to pass them by reference, as the vector is also being passed.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
I guess you can pass the pointer to the vector!!
|
|
|
|
|
I would like to share an MFC map object accross 3 processes. I am using CMapStringToString class. Any process can initialize the map at startup and I want the other processes to use a shared copy of this map. How can I do this ? I have no idea so any suggestion is welcome.
Thanks in advance
Orcun Colak
|
|
|
|
|
i don't think is possible, especially not with MFC. you might be able to do something with shared memory, but it won't be a CMap.
-c
There's one easy way to prove the effectiveness of 'letting the market decide' when it comes to environmental protection. It's spelt 'S-U-V'.
--Holgate, from Plastic
|
|
|
|
|
It would be quite difficult to do. Probably can't do it at all with a straight up CMap.
I think the only way you could share something that dynamic across multiple processes would be to set up a memory-mapped file, and map the new and delete operators for your class to use memory from that file or something.
I had a similar problem a while ago, and ended up abandoning my design because of it.
Even a broken clock is right twice a day.
|
|
|
|
|
Good day!
I have a class that holds my data.
the part of the data is used to fill a menu ( in a toolbar ), and to fill a listbox ( in a controlbar ). the data is used with the menu command, and when the user d-click in the list.
The menu and the listbox are not directly members of the mainframe and are not UI related, and the menu handlers are handled in the CView ( it's multiple view application );
I'm still not sure where to put the data ? as part of the MainFrame since it's from there I'll create the menu and the listbox, and pass a pointer to each ctor ( or some init method ). or as a global singleton ( will pollute the namespace )?
Thanks
Max.
|
|
|
|
|
I suppose that if you're following the traditional MFC Document/View architecture, that your data should reside in your CDocument-derived class. If that's too constrictive, put in your CWinApp-derived class, since it's a global singleton anyway.
|
|
|
|
|
Hi,
I have an application that adds lots of stuff into a list box.
How can I make sure that always the last item that was added will be displayed at the bottom of the list box without having to scroll to it ?
Thanks
Shay Harel
|
|
|
|
|
m_List.EnsureVisible( m_List.GetItemCount()-1, FALSE );
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
Well, sounds great but.....
I dont see this method as a member function of CListBox .
Any more ideas ?
Shay
|
|
|
|
|
It's actually a member of CListCtrl. You may consider switching to that instead -- it gives you more options. But if you can't, try using SetCurSel to select the last item. I don't remember if it causes the list to be automatically scrolled.
Regards,
Alvaro
Well done is better than well said. -- Benjamin Franklin
(I actually prefer medium-well.)
|
|
|
|
|
You should start using CListCtrl instead of CListBox. It has more features and, personally, I think it is easier to work with.
CListBox:
m_List.SetCurSel(m_List.GetCount()-1);
Win32 (works in any version of MFC):
Here is the 'old school' Win32 macro way to ensure that the last item is displayed (MFC is just a wrapper around Win32 calls, mostly):
ListView_EnsureVisible( hList, ListView_GetItemCount(hList)-1, FALSE );
Also, the macros ListView_EnsureVisible() and ListView_GetItemCount() are defined in commctrl.h
'F1' can go a long way...
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
I think what you need is (win32) LB_SETTOPINDEX. I think that's what it's called anyway.
|
|
|
|
|
I am a new programmer, can't figure out why the keyboard doesn't responds. I traced the following code, the doesn't enter the code corresponding to keyboard at all.
Here I attached how the dialog is triggered. Hopefully someone can help me figure it out. Thanks a lot.
int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow)
{
.....
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
....
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper);
}
BOOL CALLBACK Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message) {
case WM_CHAR:
switch(wParam)
{
case '1':
record_data_pair(1);
break;
case '2'://0x32:
record_data_pair(2);
break;
case '3'://0x33:
record_data_pair(1);
break;
default:
break;
}
return FALSE;
}
.....
return FALSE;
}
Sean
|
|
|
|
|
I think the problems lies in this part of your code...
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Exper);
If you specify 'NULL' as owner of this window, the Desktop will be the parent. However, since this dialogbox doesn't have it's own messageque it won't receive anything.
So, if you do it like this, it should work just fine.
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Exper);
Where 'hwnd' is the handle from your main window (in MainWndProc)
Don't think you are, know you are...
|
|
|
|
|
Shall I declare a global HWND hmainwnd; and within the WinMain() function.
if (!(hwnd = CreateWindow ("MAINWINDOW", "1DoF Stepper Motor Haptic Device",
WS_OVERLAPPEDWINDOW,
0,0,
800, 150,
NULL, NULL, hCurrentInst, NULL)))
return (0);
hmainwnd = hwnd;//like this???
and then when triggering the dialog, I do
hwnddlg = CreateDialog(app_instance, "Experiment", hmainwnd, (DLGPROC) Parameter_Window_Exper);
But I did so, it doesn't work.
The sequence triggered is as following: Main->Dialog_Data->Dialog_Experiment. I want the Dialog_Experiment to respond my keyboard.
My code(simplified) is like this:should I change all NULL to hmainwnd?
~~~~~~~~~~~~~~~~~~~~~~~~~~~`
extern HWND hwnddlg;
int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst, LPSTR lpszCmdLine, int nCmdShow)
{
...
if (!RegisterClass (&wc)) {
MessageBox (NULL, (LPCTSTR) "WinMain(): RegisterClass() failed",
(LPCTSTR) "Error!", MB_OK | MB_ICONEXCLAMATION);
return(FALSE);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
------------------------------------------
LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", NULL, (DLGPROC) Parameter_Window_Data);
}
-------------------------------------------
BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
...
DestroyWindow(hWnd);
hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, NULL, (DLGPROC) Parameter_Window_Exper);
}
-------------------------------------------
BOOL Parameter_Window_Exper(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
case WM_CHAR:
switch(wParam)
{
case '1':
MessageBox(hmainwnd, "Happen?", "TEST Box", MB_OK);
record_data_pair(1);
break;
case '2'://0x32:
record_data_pair(2);
break;
case '3'://0x33:
record_data_pair(1);
break;
default:
break;
}
return FALSE;
}
Thanks
Sean
|
|
|
|
|
I don't think you have to declare a global variable.
This should do the trick (and if not, I'll need to see more of your code):
LRESULT CALLBACK MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
...
hwnddlg = CreateDialog(app_instance, "PARAMETER_WINDOW_DATA", hwnd, (DLGPROC) Parameter_Window_Data);
}
-------------------------------------------
BOOL CALLBACK Parameter_Window_Data(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
...
DestroyWindow(hWnd);
hwnddlg = CreateDialog(app_instance, EXPER_INTRO_DIALOG, hWnd, (DLGPROC) Parameter_Window_Exper);
}
-------------------------------------------
You see, just give it the handle to the parent in stead of using NULL (which means the desktop)
Don't think you are, know you are...
|
|
|
|
|
I did so as you posted, but the child dialog (EXPER_INTRO_DIALOG) can't be displayed. I wonder if the problem now(not display at all) is because DestroyWindow(hWnd) has killed the parent dialog.
Anyway, I guess I can get my program done under your help.
Regards,
Sean
|
|
|
|
|
Hello gurus,
I have a similar question to what was discussed
here
Suppose I have a function to split a text file into 10 parts like (The first 9 parts are 90% of the total text and the last part contains whatever remaining).
When the dialog closes I have a memory leak problem related to pBuffer which I'm not releasing, as is expected. How and where do I free pBuffer?
the code goes like(not too commented)
BOOL CReadPercentDlg::ReadPercent(LPCTSTR strFileName)<br />
{<br />
HANDLE hTheFile;<br />
hTheFile = CreateFile(strFileName,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);<br />
if(!hTheFile)<br />
return FALSE;<br />
DWORD dwFileSize = ::GetFileSize(hTheFile, NULL );<br />
DWORD dw10Percent = (DWORD)( dwFileSize *.10 );<br />
BYTE *pBuffer = new BYTE[ dw10Percent ];<br />
<br />
if( !pBuffer )<br />
{<br />
TRACE("Not enough mmemory");<br />
return FALSE;
}<br />
<br />
DWORD dwStatus = 0;<br />
DWORD dwRead = 0;<br />
DWORD dwTotalRead = 0;<br />
for(int i=0;i<9;i++)<br />
{<br />
<br />
dwStatus = ::ReadFile( hTheFile, pBuffer, dw10Percent, &dwRead, NULL );<br />
if( dwStatus != ERROR_SUCCESS )<br />
{<br />
<br />
} <br />
<br />
pBuffer[dw10Percent] = '\0';<br />
OutputDebugString((LPSTR)pBuffer);<br />
dwTotalRead+=dw10Percent;<br />
}<br />
<br />
pBuffer[0] = '\0'; <br />
pBuffer = NULL;<br />
DWORD dwLeft = 0;<br />
dwLeft = dwFileSize - dwTotalRead;<br />
if(dwLeft!=0)<br />
{<br />
TRACE("\nLeft %u",dwLeft);<br />
<br />
pBuffer = new BYTE[dwLeft];<br />
if(pBuffer==NULL)<br />
return FALSE;<br />
<br />
dwStatus = ::ReadFile( hTheFile, pBuffer, dwLeft, &dwRead, NULL );<br />
if( dwStatus != ERROR_SUCCESS )<br />
{<br />
<br />
<br />
<br />
} <br />
pBuffer[dwLeft] = '\0';<br />
TRACE("\nLast :\n");<br />
TRACE((LPSTR)pBuffer);<br />
<br />
}<br />
<br />
pBuffer = NULL;<br />
return TRUE;<br />
<br />
}
Also, is there any better way to read in such percents of text files?
Thanks a lot in advance.
Looking for a lurking sig!
|
|
|
|
|
smartptr wrote:
(The first 9 parts are 90% of the total text and the last part contains whatever remaining).
smartptr wrote:
Also, is there any better way to read in such percents of text files?
If you used C++ file handling ( iostreams ), you could read into std::strings, which would clean up after themselves. Otherwise, you need to delete [] the buffer at the end of this function. There is no-where else to do it, you create it on the heap within this function, therefore once you leave this function, the pointer has leaked, you no longer have a reference to it.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
Thanks for your reply.
Can I make the buffer a member variable of the dialog class?(and when the dialog is closing:
delete pBuffer;<br />
pBuffer = NULL; )
to avoid a memory leak?
|
|
|
|
|
Sure - no problems at all, except that you need to delete [] pBuffer, not delete it, it is an array.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|