我在这里有点麻烦......我将首先从要求开始:
- 尝试向服务器发送数据(msg)
- 如果失败,将其存储在本地硬盘文件中,作为 CSV 条目列表
- 尝试在某个预定点将 msg 数据发送到服务器。
- 如果消息发送成功,则将其从文件中删除
- 继续该过程,直到向服务器发送数据失败。并转到第 2 步
我做了什么:
- 使用 fstream 对象将失败的消息写入本地文件
- 使用 fstream 对象从该文件中读取,并存储在动态创建的 std::queue 中
- 对于从文件中读取的每个味精,将其推送到队列中
- 推送所有消息后,使用 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);
// 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:
*/
谢谢,丹尼尔