|
Yonggoo wrote: How can I read in this file using while() or for()
Neither are necessary, and their use would only serve to confuse you further. You can use an iterator on a stringstream object, and then use the copy() function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I have not used an iterator on a stringstream.
Does std c++ have iterator?
Thanks!
Yonggoo
|
|
|
|
|
Yonggoo wrote: Does std c++ have iterator?
Yes.
class LineItem
{
public:
LineItem() {}
~LineItem() {}
void setLength(const int length) { _Length = length; }
int getLength() const { return _Length; }
void setNumbers(const std::vector<int>& numbers) { _Numbers.assign(numbers.begin(), numbers.end()); }
std::vector<int> getNumbers() const { return _Numbers; }
private:
int _Length;
std::vector<int> _Numbers;
};
std::ostream& operator<<(std::ostream& os, const LineItem& s)
{
os << s.getLength() << "|";
const std::vector<int> numbers = s.getNumbers();
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, LineItem& s)
{
int length;
is >> length;
std::string sNumbers = "";
std::getline(is, sNumbers);
std::stringstream ss(sNumbers);
std::vector<int> numbers;
std::copy(std::istream_iterator<int>(ss), std::istream_iterator<<int>(), std::back_inserter(numbers));
s.setLength(length);
s.setNumbers(numbers);
return is;
}
void main( void )
{
std::ifstream fin;
std::vector<LineItem> lines;
fin.open("c:\\input.txt");
std::copy(std::istream_iterator<LineItem>(fin), std::istream_iterator<LineItem>(), std::back_inserter(lines));
fin.close();
std::copy(lines.begin(), lines.end(), std::ostream_iterator<LineItem>(std::cout, "\n"));
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
The following is my code for when I click on a button that's labeled "Start". I use a functor to call my class member function pointer (via Execute(), where Execute() takes an int& parameter). When I set a breakpoint at the Execute() calls, and I hover my mouse over "i", the MSVC debugger tells me that i has some really high value like 4632955, and when I check &i in the immediate window, it tells me the memory address of i. Then, I go into Execute() to call the specific function pointer &MainWindow::DoWork, when I check the input parameter (which takes an int& as mentioned earlier), the memory address of that input parameter is &i + 4 bytes. Basically, what I'm saying is that MSVC shows me different memory address values from the calling function ButtonStart() and within the called function DoWork(). The value from ButtonStart() is wrong, as my int is not some really high number, but the value that is actually received in DoWork() is right, and ends up being 5 (what I set it to in the beginning). This is the first problem and I don't know why this is happening.
The second problem is that some of my lines of execution are being skipped. Actually, that is inaccurate, as they are being executed but I cannot set a breakpoint at some of the lines such as the line int i = 5. MSVC will just shift my breakpoint down to the Execute() calls. My debugging info is turned on, and I'm am building in Debug mode, so this shouldn't happen (I've had it happen when building in Release mode).
void MainWindow::ButtonStart()
{
if (!m_threadPool && !m_source && !m_receiver)
{
m_source = new EventSource();
m_receiver = new EventReceiver();
m_threadPool = new ThreadPool(m_source, m_receiver);
m_receiver->HookThreadPool(m_source);
int i = 5;
CallbackWrapperSpecific<MainWindow, void (MainWindow::*)(int&), int> cbwsPtr(this, &MainWindow::DoWork);
CallbackWrapperBase<int>* cbwbPtr = &cbwsPtr;
cbwbPtr->Execute(i);
cbwbPtr->Execute(i);
}
}
|
|
|
|
|
Is a Rebuild All in order?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yep, I've done multiple cleans and rebuilds -- still no luck. I wonder if I've corrupted the memory somewhere... or I need to enable some kind of extra flag for debugging MFC Multithreaded applications, etc.
|
|
|
|
|
Bah, it was just optimizing for maximum speed (which was a default setting) that caused these problems... I turned it off (/Od) and now I can see everything the way I want to for a debug version.
|
|
|
|
|
Cyrilix wrote: Bah, it was just optimizing...
In debug mode?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Yes, it was the default in debug mode when I created the project.
|
|
|
|
|
<>.,yuutyutyutyu,/,.//,.//,./<.<><<>,<>
';l';l
|
|
|
|
|
Will you stop spamming the forums with your crap please ?
|
|
|
|
|
Dear all
i need your help. in advance i thank u. my issue is as follows:
if i have a menuitem, after i click it, one dialog appears:ask users to enter parameters below: length=10 pixel, width=10pixel, height=10 pixel, then click ok.correspording to cube will show in window. how to program this point? who have any such snippet? so urgent. thanks very much.
regards
zhiyuan
Li Zhiyuan
6/14/2007
|
|
|
|
|
li zhiyuan wrote: who have any such snippet?
My good friend the internet [^] does.
|
|
|
|
|
I am tiered of banging my head against the wall trying to figure this out. So I am here to find out from the experts what the problem might be. The following code:
void CMonthCalCtrlEx::OnMouseMove(UINT nFlags, CPoint point) <br />
{<br />
MCHITTESTINFO mcHitTest;<br />
mcHitTest.cbSize = sizeof(MCHITTESTINFO);<br />
mcHitTest.pt = point;<br />
<br />
switch(HitTest(&mcHitTest))<br />
{<br />
case MCHT_CALENDARDATE:<br />
case MCHT_CALENDARDATENEXT:<br />
case MCHT_CALENDARDATEPREV:<br />
{<br />
if(memcmp(&mcHitTest.st, &m_stOldToolTipDate, sizeof(SYSTEMTIME)))<br />
{<br />
m_tooltip.Activate(TRUE);<br />
<br />
if(m_mapHolidayNames.Lookup(mcHitTest.st.wYear * 10000 + mcHitTest.st.wMonth * 100 + mcHitTest.st.wDay, strToolTip))<br />
{<br />
m_tooltip.Activate(TRUE);<br />
m_tooltip.UpdateTipText(strToolTip, this);<br />
<br />
MSG msg;<br />
msg.hwnd = m_hWnd;<br />
msg.message = WM_MOUSEMOVE;<br />
m_tooltip.RelayEvent(&msg);<br />
}<br />
else<br />
{<br />
ExtndedToolTip(mcHitTest);<br />
m_tooltip.UpdateTipText(strToolTip,this);<br />
MSG msg;<br />
msg.hwnd = m_hWnd;<br />
msg.message = WM_MOUSEMOVE;<br />
m_tooltip.RelayEvent(&msg);<br />
}<br />
}<br />
break;<br />
}<br />
default:<br />
{<br />
memset(&m_stOldToolTipDate, 0, sizeof m_stOldToolTipDate);<br />
m_tooltip.Activate(FALSE);<br />
break;<br />
}<br />
}<br />
CMonthCalCtrl::OnMouseMove(nFlags, point);<br />
}
worked fine under Microsoft Visual C++ 6.0. However, I have upgraded to Visual Studio C++ 2005 and now the code fails. Can anyone please point in in the right direction on how to fix it in VS? Everything compiles ok, but it seems to by pass this MCHITTESTINFO mcHitTest; and does not assign the point value to mcHitTest.pt = point;. So do I have to use some other method for dealing with OnMouseMove in VS? Any help would be appreciated.
Thanks,
Charlie
Everything is Free... Until You Have to Pay for it...
Platforms Windows 2000/XP Professional using Visual C++ 6.0
|
|
|
|
|
I'm not sure if this is the problem, but the point passed to OnMouseMove is in client coordinates.
Is the MCM_HITTEST (HitTest()) message expecting screen coordinates?
Never mind - I tested your code and it worked for me...
Mark
-- modified at 13:39 Wednesday 13th June, 2007
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hey,
I've been busy with C++ for many years now, and this particular problem has always been eluding me.
I'm using Visual C++ 6.0, with Service packs 5&6 installed, as well as processor pack 5.
My machine is running Windows XP SP2, and hyperthreading is enabled.
To Illustrate my problem I've created a simple test app. :
In a multi-threaded C++ Application, I have a Parent Dialog, which creates(runs) 2 independent threads.
I have assigned each of the 2 threads, intensive, continuous computational work within a while loop. (The loop criteria is met and breaks when the parent sets a member value requesting them to exit)
This Test : starting and running both threads simultaneously and then shutting them down again, works perfectly, in debug.
However, once I compile and run the project in release, neither of the threads seem to register that the the loop criteria variable has changed.
------------------------------------------------------
Thread Code :
------------------------------------------------------
CTestThread1Dlg* parent = (CTestThread1Dlg*) param;
parent->m_Thread1Running = true;
parent->m_Thread1Starting = false;
while (parent->m_RunThread1)
{
parent->m_Th1Value++;
}
parent->m_Thread1Running = false;
_endthread();
------------------------------------------------------
But this only happens when the thread is using all the computational power that it can get (no interruptions, ie. conditionals, Sleeps, etc).
If I were to introduce a "Sleep(15)" right after the increment, then the thread would immediately register a change in "m_RunThread1" and exit, the moment the parent changes "m_RunThread1".
With the given code above(Thread Code), in release, the thread does not register and break the while loop when I call code from the parent, such as :
------------------------------------------------------
Parent Code :
------------------------------------------------------
m_RunThread1 = false;
Beep(450, 50);
Beep(450, 50);
while (m_Thread1Running || m_Thread1Starting)
Sleep(100);
AfxMessageBox("Thread1 has exited");
------------------------------------------------------
I added the 2 beeps so that I also have audio confirmation that "m_RunThread1", was indeed set - and I hear the beeps, but the threads keep going.
Do the threads have a type of duplicate memory state of all the members that they access, (which are only updated once the thread's CPU time has elapsed, and it is removed from the stack for the next thread)?
That does not make logical sense to me, but it is the only reason that I could think of, explaining why the parents' "parent->m_RunThread1" is not 'updated' until the thread's execution cycle is interrupted...
Has anyone else experienced this behavior?
I've currently pinned down the problem within a huge multi-threaded application, and unless I can identify how to solve it in this simple test case, there is no hope of fixing it :/
Any info. would be appreciated, thanks.
|
|
|
|
|
Caching of memory or other optimizations may cause problems like this, which is why the volatile keyword exists. Try declaring the m_RunThread* variables as volatile and see if that works.
You can also try turning off optimizations for the sections of code involved. (First try building a release build with optimizations turned off for the files containing the code the threads are running.)
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Thanks James,
Both of your answers solved the problem.
I'll probably resort to using the volatile qualifier.
After checking the documentation, I realized that both solutions you mentioned point to the same reason : optimization.
Here's what the documentation for the volatile qualifier said :
"Objects declared as volatile are not used in optimizations because their value can change at any time. The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment."
Incredible... I was never under the impression that optimizations performed such reckless(for lack of a better word :P) operations!
I was always under the misguided impression that changing the value of a member would be executed with immediate and accurate effect - almost feel like using the volatile qualifier on every single multi-threaded member that I use now!...
Just another question though -
The documentation states : "The following example declares a volatile integer nVint whose value can be modified by external processes.... "
Does this imply that without the volatile keyword, that memory would otherwise be guarded against other processes trying to attempting to modify it?
Also (and only if you know off hand) - would you be able to point me in the direction of some good reading on this topic (optimal multi-threaded interaction)?
Thanks again.
|
|
|
|
|
The compiler will optimize and not read the actual value of a variable if the value is not changed within the scope (e.g. a while-loop). This is why the volatile keyword is essential in this case.
In your implementation you will also get corrupted data. You won't see the effect of it since you don't use the parent->m_Th1Value variable. This should be guarded by a critical section.
Eshazear wrote: The documentation states : "The following example declares a volatile integer nVint whose value can be modified by external processes.... "
Does this imply that without the volatile keyword, that memory would otherwise be guarded against other processes trying to attempting to modify it?
No.
The volatile keyword informs the compiler that the variable may change in the background and thus the actual value of the variable has to be read and cannot be optimized.
Protecting the data written to by different processes is done by using data synchronization objects such as critical sections and mutexes.
Eshazear wrote: would you be able to point me in the direction of some good reading on this topic
Absolutely, the best place to start I've found on the net is here[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Thanks - that will help a great deal
|
|
|
|
|
I've searched with google a bit, but can't find any info. about this.
In the Project Application (which I wrote the test app. for), I made use of dynamic arrays, whose size is only decided at run time.
Fortunately, the previous solution showed how the volatile qualifier can be used to force a memory lookup for a specific variable when it is read -
however, I need a handful of dynamic arrays to be volatile - and the memory allocation tells me that I "cannot allocate 'const'/'volatile' objects".
Simply declaring : bool* volatile m_NewReadyFrameFlag;
and later allocating m_NewReadyFrameFlag = new bool[CurChs];
only means that the pointer to the array itself is volatile and not the individual elements...
which is the best way around this (ie. to create a dynamic volatile array)?
|
|
|
|
|
Hi All
I want to find the icon associated with all the file extensions and if no valid extension is there then find the generic icon provided by the system for unknown file extension.
Please help me to do this...
Thanks
|
|
|
|
|
Why you post three messages for a question?
|
|
|
|
|
For your question do you need to SHGetFileInfo ?
|
|
|
|
|
Hi
Thanks for your reply
Infact what I want is to find the icon of a file which is i am recieving from the remote system so I have ony file name (e.g. Test.txt, Test.pdf, etc)
So I need to find the icon associated with file extensions like .txt, .pdf, etc.
that means I dont have full path of the file...
So please help me for solving this problem....
Thanks & Regards
|
|
|
|
|