我想創建一個類似於ifstream的解析緩衝區類型。
你可以嘗試這樣的事情(從link採納我的意見提供了的話):
std::ifstream ifs("test.txt", std::ifstream::binary);
if (ifs)
{
ifs.seekg (0, ifs.end);
int length = ifs.tellg();
ifs.seekg (0, ifs.beg);
std::string buffer;
buffer.resize(length);
ifs.read(const_cast<char*>(buffer.data()), length);
if (ifs)
{
// de-crypt the buffer here!
// something like:
// buffer[i] = decryptChar(buffer[i]);
std::istringstream iss(buffer);
// now you can use iss just the same way as ifs,
// if the file was not encrypted...
}
else
{
std::cout << "error: only " << ifs.gcount() << " bytes could be read";
}
ifs.close();
}
編輯迴應您的評論:
std::istringstream
用於將文本轉換成二進制數據,例如, G。 int n; iss >> n;
會將ascii序列0x32,0x30,0x31,0x30,0x32,0x30,0x31,0x32表示的字符串「20102012」轉換爲相應的四字節整數值0x0132bb7c。但是如果數據是已經是二進制的,則std::istringstream
不適用。這時,你可能寧願嘗試寫類似這樣的例子你自己的流類:
class DecryptionStream
{
std::unique_ptr<char> mBuffer;
char* mEnd;
char* mPos;
unsigned int flags;
unsigned int const eofbit = 1 << 0;
unsigned int const failbit = 1 << 1;
// other flag bits as needed
public:
// fail/eof bits as needed
DecryptionStream(char const* fileName) : mPos(nullptr)
{
std::ifstream ifs(fileName, std::ifstream::binary);
if (ifs)
{
ifs.seekg (0, ifs.end);
int length = ifs.tellg();
ifs.seekg (0, ifs.beg);
mBuffer.reset(new char[length]);
ifs.read(mBuffer.get(), length);
if (ifs)
{
// de-crypt the buffer here!
// something like:
// buffer[i] = decryptChar(buffer[i]);
mPos = mBuffer.get();
mEnd = mBuffer.get() + length;
}
else
{
flags |= failbit;
}
ifs.close();
}
}
template<typename T>
DecryptionStream& operator >>(T& t)
{
// fail, if any fail bit set already
size_t avail = mPos - mEnd;
if (avail < sizeof(t))
{
flags |= eofbit | failbit;
}
else
{
if(avail == sizeof(t))
{
flags |= eofbit;
}
memcpy(&t, mPos, sizeof(t));
mPos += sizeof(t);
}
return *this;
}
operator bool()
{
return flags == 0;
}
};
你甚至可以使用這個類具有複雜數據類型 - 然後確保,雖然,你適當地控制這些字節對齊,否則你可能會失敗!
你爲什麼不使用寫? 'handle2.write(buffer,size);' – Aconcagua
我試過了,它沒有工作......如果我嘗試讀取它沒有的緩衝區。 – d3vil401
你是如何打開你的fstream的? fstreams旨在將數據寫入文件或讀取數據。如果它沒有連接到一個文件,你將不會得到任何輸出。但是你究竟想要在現實中達到什麼目的?將整個文件內容讀入某種數據緩衝區? ifstream讀取文件的內容,您可以閱讀它。 G。逐字節通過'char c;流>> c;'。 – Aconcagua