C++ ifstream.read;读取的字节数少于给定的 n 个字节
我正在尝试将数据分成小数据包。我不太确定这个读取方法是如何工作的,但我已经给出了 512 的缓冲区大小来从文件中读取。 但我没有得到 512,而是在我的第一个数据包中得到了 5。其他的范围从 0 到 512 以上(这不应该发生)。
这是一个我正在尝试拆分的 zip 文件: 在文本中,前几个字节如下所示
(类似 de Bucket 的字符实际上是 2 个字符)
它似乎按预期抓取了前 5 个字节,但之后只是停止并转到下一个读取块。
因为它是一个 512 的缓冲区,前 5 个字节之后的所有内容都是垃圾。 我正在使用 ifstream。并且模式设置为二进制。
有什么建议吗?
void FileProcessor::send()
{
//If no file is opened return
if(!_file.is_open()) return;
//Reset position to beginning
_file.seekg(0, ios::beg);
//Result buffer
char * buffer;
char * partBytes = new char[_bufferSize];
//Read the file and send it over the network
while (_file.read(partBytes, _bufferSize))
{
buffer = Packet::create(Packet::FILE,partBytes);
Packet *p = Packet::create(buffer);
//cout << strlen(partBytes);
//p->PrintHex(buffer,_bufferSize+Packet::HeaderSize);
//break;
cout << "Normal size : \t" << strlen(partBytes)<< "\tPacketSize: \t" << p->getLength()<<"\n";
//cout << strcmp(p->getData().c_str(),partBytes) << "\n";
writeToFile(p->getData().c_str(),p->getData().length());
delete p;
}
//Write final bytes if any
if(_file.gcount())
{
//writeToFile(partBytes, _file.gcount());
buffer = Packet::create(Packet::FILE,partBytes);
Packet *p = Packet::create(buffer);
writeToFile(p->getData().c_str(),p->getData().length());
//cout << p->getLength() << "\n";
delete p;
}
//cout<< *p << "\n";
delete [] partBytes;
}
我现在只是测试直接读写。
I'm trying to split up data into little packets. I'm not exactly sure how this read method is suppose to work but I've given a buffer size of 512 to read from the file.
But instead of getting 512 i just get 5 in my first packet. Others vary from 0 to above 512 ( which shouldn't happen).
It's a zip file I'm trying to split up:
In text the first few bytes look like this
(de bucket like characters are actually 2 characters)
It seems to grab the first 5 bytes as it should but afterwards just stops and goes to the next read block.
Since it's a buffer of 512 everything after the first 5 bytes is garbage.
I'm using an ifstream. And the mode is set to Binary.
Any suggestions?
void FileProcessor::send()
{
//If no file is opened return
if(!_file.is_open()) return;
//Reset position to beginning
_file.seekg(0, ios::beg);
//Result buffer
char * buffer;
char * partBytes = new char[_bufferSize];
//Read the file and send it over the network
while (_file.read(partBytes, _bufferSize))
{
buffer = Packet::create(Packet::FILE,partBytes);
Packet *p = Packet::create(buffer);
//cout << strlen(partBytes);
//p->PrintHex(buffer,_bufferSize+Packet::HeaderSize);
//break;
cout << "Normal size : \t" << strlen(partBytes)<< "\tPacketSize: \t" << p->getLength()<<"\n";
//cout << strcmp(p->getData().c_str(),partBytes) << "\n";
writeToFile(p->getData().c_str(),p->getData().length());
delete p;
}
//Write final bytes if any
if(_file.gcount())
{
//writeToFile(partBytes, _file.gcount());
buffer = Packet::create(Packet::FILE,partBytes);
Packet *p = Packet::create(buffer);
writeToFile(p->getData().c_str(),p->getData().length());
//cout << p->getLength() << "\n";
delete p;
}
//cout<< *p << "\n";
delete [] partBytes;
}
Im just testing a direct read write right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在循环内,不要假设它总是读取完整的数据缓冲区,而是使用 gcount() 来查找它实际读取的数据量,并传输该数据量。
我觉得有必要补充一点:这
对我来说看起来很奇怪。不确定这是错误的,但也不能立即明显看出它是正确的(如果是正确的,设计似乎有点奇怪)。
我还会跳过动态分配和删除:
并使用
std::vector
代替。Inside your loop, instead of assuming it always reads a full buffer of data, use
gcount()
to find how many it actually read, and transmit that many.I feel obliged to add that this:
looks quite strange to me. Not sure it's wrong, but it's not immediately obvious that it's right either (and if it is right, the design seems a bit odd).
I'd also skip the dynamic allocation and deletion:
and use a
std::vector<char>
instead.