推送后获取正确文本,然后从 std::queue 获取 front() 时出错

发布于 2024-10-01 21:07:38 字数 2550 浏览 1 评论 0原文

我在这里遇到了一些麻烦...我首先从要求开始:

  1. 尝试将数据(消息)发送到服务器
  2. 如果失败,将其存储在本地硬盘文件中,作为 CSV 条目列表
  3. 尝试在某个预定点将消息数据发送到服务器。
  4. 如果消息发送成功,则将其从文件中删除
  5. 继续该过程,直到向服务器发送数据失败。并转到步骤 2

我所做的:

  1. 使用 fstream 对象将失败的消息写入本地文件
  2. 使用 fstream 对象从此文件读取数据,并将其存储在动态创建的 std::queue 中
  3. 对于从文件读取的每个消息,推送在队列中
  4. 推送所有消息后,使用 std::front() 获取第一个消息,并将其读入自定义对象数据结构中。

问题是: 我在将硬盘文件推入队列之前和之后打印从硬盘文件读取的消息。在推送队列之前,我打印到消息框/文本文件日志中的数据绝对没问题。但是当我在获取queue:front()后打印相同的数据时,它会打印所有垃圾。*

我不是队列和STL方面的专家,所以我需要指导。

代码如下:

 
    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);

// 我写入文件并从文件读回的 CSV 如下所示: // 1111222233334444,1234,05,0011123456,20100102112233,1234567890,7,N

// 下面是我写入文件的方式: void CDataQueueingAndPersisting::WriteQueueMsgsToFile(char *pchAppendMsgToPersistentFile) { char chWriteBuffer[512] = {0}; fstream_IOData_Write.flush(); sprintf(chWriteBuffer, "%s\r\n", pchAppendMsgToPersistentFile); if(NULL != pchAppendMsgToPercientFile) fstream_IOData_Write.write(chWriteBuffer,strlen(chWriteBuffer)); }

// 下面是我从文件中读取的方式: while(fstream_IOData_Read>>chSingleDFCMsg) { bDataRead = ReplicateQueueInProcessMemory( (BYTE*) chSingleDFCMsg); RtlZeroMemory(chSingleDFCMsg, sizeof(chSingleDFCMsg)); }

// ReplicateQueueInProcessMemory 就像: pChDelimitedStrPtr = strtok((char *)byteSingleRawQueueMsg, ",");

// 读取单行中的每个逗号分隔字段,如上所示。我使用多个 strtok() 来读取字符串的所有字段。

// 之后我得到队列中的最前面的消息: CDFCQueueMsgs oDfcQueueMsg_TopMsg; CDFCQueueMsgs & refDfcQueueMsg_TopMsg = oDfcQueueMsg_TopMsg; refDfcQueueMsg_TopMsg = *oCDataQueueingAndPersisting.poDFCMsgQUEUE.front();

// 现在我获取队列保存的对象类型的相应成员字段: strncpy(g_chBuffer, refDfcQueueMsg_TopMsg.chDFCMsg_1, sizeof(refDfcQueueMsg_TopMsg.chDFCMsg_1));

// 现在我在日志文件中记录“g_chBuffer”变量。我还在日志中记录了每个字段: /* 在推入队列之前,我记录读取缓冲区中的字符串,字段被记录得很好,如下所示: 09:50:45:093 事件:chDFCMsg_1:1111222233334444 chDFCMsg_2:1234 chDFCMsg_3:05 chDFCMsg_4:0011123456 chDFCMsg_5:20100102112233

推送并获取 Queue::front() 后,我看到相同的字段,如下所示: 10:45:54:495 事件:2ÛS 10:45:54:495 事件: ØS 10:45:54:495 事件:á 10:45:54:495 活动: 10:45:54:495 事件: */

谢谢, 丹尼尔

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 dynamically 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-10-08 21:07:38

oCDataQueueingAndPersisting 的内容是什么? (尤其是 poDFCMsgQUEUE 的类型)

如果我是对的:
* 我认为它实际上是一个指针队列而不是数据队列。
* 这意味着 poDFCMsgQUEUE.front() 指向的内存不是好的内存。

例如你不能这样做:

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

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

在这种情况下,myItem 在 function1 返回后被销毁。所以 myQueueOfPointer::front() 中的地址仍然指向任何东西(可以被其他函数等使用的内存)。这就是为什么你在第二次打印时打印出一块垃圾。第一个成功是因为内存尚未被程序的其他部分覆盖。

PS:

正如我们都要求的那样,您没有发布将元素推送到队列中的部分。我会最后告诉你一次,但如果你不发布它,我们无法帮助你(甚至相反,你的帖子会被否决)。

对未来的建议(如果你想有效地使用这个网站):

  • 很好地呈现你的问题(这没问题)
  • 粘贴你的问题的源代码
    • 缩进良好,结构合理(就像我在这篇文章中所做的那样)
    • 如果很复杂:尽可能减少源代码(只保留有趣的部分)

希望它会对您有所帮助

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

甜扑 2024-10-08 21:07:38

std::queue::front 检索第一个条目的值或其引用。

所以你的代码:

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

应该是:

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文