要件 :
特定のファイルからEOF(一度に16バイト)まで読み取ってから、5秒間スリープと言う必要があります。さて、5秒後、ファイル(その時点で内容が追加されている)から読み込もうとすると、意図したデザインは、前に残した場所から読み取り、内容を再度スキャンするようになっている必要があります。 (一度に16バイト)EOFに達するまで。
次のようにifstreamを使用して、指定されたファイルから(EOFまで-16バイトずつ)読み取る(基本的な)コードを記述しました。
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int fd, i, j, length, pos;
char buffer[100][16];
ifstream Read;
std::ostringstream oss;
int current_position = 0;
Read.open("from4to5", ios::binary);
//Get the size of the file
Read.seekg(0, ios::end);
length = Read.tellg();
Read.seekg(0, ios::beg);
for(i=0; i<length; i++)
{
buffer[i][16] = '\0';
}
//Read the file in 16byte segments or eof(), whichever comes first
//Testing the return condition of the function is preferred, as opposed to testing eof()
while(Read.get(buffer[i], 17))
{
for(j=0; j<=16; j++)
oss << buffer[i][j];
cout << "Contents : " << oss.str() << endl;
oss.seekp(0);
i++;
}
// Output is :
// Contents : BD8d3700indiaC#E
// Contents : BD6d4700godgeD3E
// Contents : BD9d1311badge3TE
return 0;
}
要件に合わせてこれを変更する必要があります。seekg()呼び出しを使用してみましたが、どういうわけか失敗しました。初めてファイルにアクセスしてファイルストリームに読み込んだときに、プログラムがファイルに排他ロックを設定したのではないかと思っていました。つまり、次回はファイルから読み取ることができなくなります。 。
誰かがそれがどのように行われるべきかを私に示すことができますか?
ファイル名: "from4to5"内容:
BD8d3700indiaC#EBD6d4700godgeD3EBD9d1311badge3TE
5秒以内に、他のプロセスが同じファイル「from4to5」に書き込み(追加)し ます。
ファイルの内容:
BD8d3700indiaC#EBD6d4700godgeD3EBD9d1311badge3TEBD6d1210clerk41EBD2d1100mayor47EBD4d2810bread6YE
これで、プログラムがファイル「from4to5」から読み取る場合、EOFに遭遇するまで、前に離れた場所から16バイトずつ読み取る必要があります。
今回の出力の意図は次のとおりです。
// Output is :
// Contents : BD6d1210clerk41E
// Contents : BD2d1100mayor47E
// Contents : BD4d2810bread6YE