-2

私はここで少し困っています...最初に要件から始めます:

  1. データ (msg) をサーバーに送信してみる
  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);

// 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: */

ありがとう、ダニエル

4

2 に答える 2

1

oCDataQueueingAndPersisting の内容は何ですか? (特に poDFCMsgQUEUE のタイプ)

私が正しければ: * 実際にはポインタのキューであり、データのキューではないと思います。* これは、poDFCMsgQUEUE.front() が指すメモリーが適切でないことを意味します。

たとえば、それはできません:

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

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

この場合、myItem は function1 が戻った後に破棄されます。したがって、 myQueueOfPointer::front() のアドレスはまだ何も指していません (他の関数などで使用できるメモリなど)。これが、2 番目の印刷でがらくたを印刷する理由です。メモリがプログラムの別の部分によってまだ上書きされていないため、最初のものは成功しました。

PS:

皆様のご要望通り、要素をプッシュする部分をキューに入れていませんでした。最後にもう一度お伝えしますが、投稿しない場合、私たちはあなたを助けることはできません (反対の場合でも、あなたの投稿は反対票を投じられます)。

今後のアドバイス (このサイトを効率的に使用したい場合):

  • あなたの問題をうまく提示してください(これは大丈夫でした)
  • 問題のソースコードを貼り付けます
    • よくインデントされ、構造化されている (この投稿で行ったように)
    • 複雑な場合:ソースコードをできるだけ減らす(興味深い部分だけで)

それがあなたを助けることを願っています

于 2010-11-12T04:24:48.627 に答える
0

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();
于 2010-11-11T04:05:44.293 に答える