开发者

Error getting correct text after pushing, and then getting the front() from the std::queue

I am in a bit of a trouble here... I will start with the requirements first:

  1. Try to send data (msg) to the server
  2. If it fails, store it in a local hard disk file, as a list of CSV entries
  3. Try to send the msg data to the server at some predetermined point.
  4. If a msg is sent successfully, remove it from the file
  5. Continue the process till sending data to server fails. and go to step 2

What I have done:

  1. Used fstream object to write the failed msgs to a local file
  2. Used fstream object to read from this file, and store in a dyna开发者_JAVA百科mically created std::queue
  3. For each msg read from the file, push it in the queue
  4. After pushing all msgs, take the first msg using std::front(), and read it into the custom object data structure.

The problem is: I print the msgs read from the hard disk file before and after pushing it into the queue. Before pushing the queue, the data I print into a messageBox/text file logs is absolutely fine. But when I print the same data after getting the queue:front() it prints all junk.*

I am not an expert on queues and STLs, so I need a guiding hand.

The code is as follows:

 
    class CDFCQueueMsgs
    {
    public:
        char chDFCMsg_1;
        char chDFCMsg_2;
        char chDFCMsg_3;
        char chDFCMsg_4;
        char chDFCMsg_5;
    };
// This is how I created the fstream obj to read the file
    fstream_IOData_Read.open(pChPersistingFileLocation, ios::in);

// The CSVs that I write to and read back from the file are like: // 1111222233334444,1234,05,0011123456,20100102112233,1234567890,7,N

// Given below is how I write to the file: void CDataQueueingAndPersisting::WriteQueueMsgsToFile(char *pchAppendMsgToPersistentFile) { char chWriteBuffer[512] = {0}; fstream_IOData_Write.flush(); sprintf(chWriteBuffer, "%s\r\n", pchAppendMsgToPersistentFile); if(NULL != pchAppendMsgToPersistentFile) fstream_IOData_Write.write(chWriteBuffer,strlen(chWriteBuffer)); }

// Given below is how I read from the file: while(fstream_IOData_Read >> chSingleDFCMsg) { bDataRead = ReplicateQueueInProcessMemory( (BYTE*) chSingleDFCMsg); RtlZeroMemory(chSingleDFCMsg, sizeof(chSingleDFCMsg)); }

// ReplicateQueueInProcessMemory is like: pChDelimitedStrPtr = strtok((char *)byteSingleRawQueueMsg, ",");

// to read every comma delimited field in the single line as shown above. I use multiple strtok()s to read all the fields of the string.

// After this I get the front message in the queue: CDFCQueueMsgs oDfcQueueMsg_TopMsg; CDFCQueueMsgs & refDfcQueueMsg_TopMsg = oDfcQueueMsg_TopMsg; refDfcQueueMsg_TopMsg = *oCDataQueueingAndPersisting.poDFCMsgQUEUE.front();

// Now I get the respective member fields to the object type the queue holds: strncpy(g_chBuffer, refDfcQueueMsg_TopMsg.chDFCMsg_1, sizeof(refDfcQueueMsg_TopMsg.chDFCMsg_1));

// Now I Log the "g_chBuffer" variable in my log files. I also log each field in my logs: /* Before Pushing into queue, I log the string from the read buffer, the fields get logged fine like this: 09:50:45:093 EVENT: chDFCMsg_1:1111222233334444 chDFCMsg_2:1234 chDFCMsg_3:05 chDFCMsg_4:0011123456 chDFCMsg_5:20100102112233

After pushing and gettting the Queue::front() I see the same fields like this: 10:45:54:495 EVENT: 2ÃÛ¬S 10:45:54:495 EVENT: ¬S 10:45:54:495 EVENT:á 10:45:54:495 EVENT: 10:45:54:495 EVENT: */

Thanks, Daniel


what is the content of oCDataQueueingAndPersisting? (Especially the type of poDFCMsgQUEUE)

If I am right: * I think it is actually a queue of pointer and not a queue of data. * which mean that the memory pointed by poDFCMsgQUEUE.front() is not the good one.

for example you can not do that:

void function1()
{
  myItem i;
  myQueueOfPointer.push(&myItem)
}

void main()
{
  function1()
  cout << myQueueOfPointer.front() // error
}

In this case, myItem is destroy after function1 return. So the address in myQueueOfPointer::front() is still pointing at nothing (a memory that can be use by other function etc...). This is why you print a piece of junk in your second print. The first one succeded because the memory was not yet overwrite by another part of your program.

PS:

As we all did request, you did not post the part where you push element in the queue. I will tell you one last time but if you do not post it, we can not help you (even the opposite, your post get downvoted).

An advice for the future (if you want to use this site efficiently):

  • Present well you problem (this was ok)
  • Paste the source code of your problem
    • Well indented, ans structured (Like i did in this post)
    • If it is complex: reduce the source code as much as possible (with only interesting part)

Hope it will help you


std::queue::front retrieve the Value of the first entry or its Reference.

So your code:

CDFCQueueMsgs oDfcQueueMsg_TopMsg;
CDFCQueueMsgs * poDfcQueueMsg_TopMsg = & oDfcQueueMsg_TopMsg;
poDfcQueueMsg_TopMsg = oCDataQueueingAndPersisting.poDFCMsgQUEUE.front();

should be:

// take the reference of the queue::front in oDfcQueueMsg_TopMsg 
CDFCQueueMsgs& oDfcQueueMsg_TopMsg;
oDfcQueueMsg_TopMsg = oCDataQueueingAndPersisting.poDFCMsgQUEUE.front();

or

// copy the queue::front() into oDfcQueueMsg_TopMsg
CDFCQueueMsgs oDfcQueueMsg_TopMsg;
oDfcQueueMsg_TopMsg = oCDataQueueingAndPersisting.poDFCMsgQUEUE.front();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜