一百分求C++ 从istream到buffer的方法

Caeserxu 2006-10-10 01:52:16
不知道流的长度,如果用read ,seek请写出具体方法
...全文
474 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinall 2006-10-10
  • 打赏
  • 举报
回复
ft,看的不是一个地方。
HRESULT Seek(
LARGE_INTEGER dlibMove, //Offset relative to dwOrigin
DWORD dwOrigin, //Specifies the origin for the offset
ULARGE_INTEGER * plibNewPosition
//Pointer to location containing new seek
// pointer
);
那么,用plibNewPosition也是一样,它是个out参数。

taodm 2006-10-10
  • 打赏
  • 举报
回复
呃,楼上的,seek的返回值
S_OK
The seek pointer has been successfully adjusted.
E_PENDING
Asynchronous Storage only: Part or all of the stream's data is currently unavailable. For more information see IFillLockBytes and Asynchronous Storage.
STG_E_INVALIDPOINTER
The value of the plibNewPosition parameter is not valid.
STG_E_INVALIDFUNCTION
The value of the dwOrigin parameter is not valid.
STG_E_REVERTED
The object has been invalidated by a revert operation above it in the transaction tree.
用seek的回传参数还差不多。
sinall 2006-10-10
  • 打赏
  • 举报
回复
晕……
“不知道流的长度,如果用read ,seek请写出具体方法”
一直以为你要“流的长度”。
好,“流的长度”我已经告诉你了^_^

Read
public int Read(byte buf[], int off, int len);


Reads bytes from the stream object into a byte array, starting at a specified offset from the current seek pointer.

Return Value:

Returns the number of bytes actually read.

Parameter Description
buf The buffer that the bytes are read into.
off The offset in the stream to begin reading from.
len The number of bytes to read.

————————————————————————————————————————
IStream * pStream;
long sz = pStream->Seek(0, STREAM_SEEK_END);
byte *buf = new byte[sz];
pStream->Seek(0, STREAM_SEEK_SET);
pStream->Read(buf, 0, sz);
Caeserxu 2006-10-10
  • 打赏
  • 举报
回复
我只想知道IStream到BYTE*的方法
IStream并不知道长度。象sinall兄的感觉还是有点远.
标题上的应该是IStream,
IStream和istream好象不太一样。
taodm 2006-10-10
  • 打赏
  • 举报
回复
楼主,别把VC专有的东西放C++版来讨论,去VC版问。
不然就自己查msdn的帮助。
sinall 2006-10-10
  • 打赏
  • 举报
回复
-_-!!!
试试pStream->Seek(0, STREAM_SEEK_END),这可能就是你要的了。
Seek返回long。
sinall 2006-10-10
  • 打赏
  • 举报
回复
唉……此IStream非彼istream……

Seek
public long Seek(long dlibMove, int dwOrigin);


Changes the seek pointer to a new location relative to the beginning of the stream, the end of the stream, or the current seek pointer.

Return Value:

Returns the new seek pointer of the stream.

Parameter Description
dlibMove The offset relative to dwOrigin.
dwOrigin The origin of the offset; must be STREAM_SEEK_CUR, STREAM_SEEK_END, or STREAM_SEEK_SET.

你可以试试:
pStream->Seek(0, STREAM_SEEK_END) - pStream->Seek(0, STREAM_SEEK_SET)
sinall 2006-10-10
  • 打赏
  • 举报
回复
找了两个类似代码,楼主参考:
1)
ifstream isXml(strFileName.c_str());
isXml.unsetf(ios::skipws);

long size = isXml.rdbuf()->pubseekoff(0, ios::end, ios::in);
isXml.seekg(0);
if (size > 0)
{
data = new char[size];
isXml.read(data, size);
}
2)
ifstream inputFile("interestingData.txt");
inputFile.unset(ios::skipws); // 关闭inputFile的
// 忽略空格标志
string fileData((istream_iterator<char>(inputFile)), istream_iterator<char>());
Caeserxu 2006-10-10
  • 打赏
  • 举报
回复
taodm兄的解释在下没看明白
具体是这样
IStream * pStream;
HGLOBAL hg;
HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
pImage->Save(pStream, ImageFormatJPEG);
GetHGlobalFromStream(pStream, &hg);
BYTE * pBuffer = (BYTE*)GlobalLock(hg);
GlobalUnlock(hg);
这个pBuffer的长度如何得知,用sizeof恐怕只有4
pImage是一个CImage类。
lann64 2006-10-10
  • 打赏
  • 举报
回复
用istream_iterator读?
taodm 2006-10-10
  • 打赏
  • 举报
回复
呃,和C的FILE取文件大小方法相同
curpos = mystream.tellp();
mystream.seekp(0, ios_base::end);
size = mystream.tellp();
mystream.fseek(curpos, ios_base::begin);
return length;
Caeserxu 2006-10-10
  • 打赏
  • 举报
回复
此IStream为C++中的流和cin好象不是很相同。
sinall 2006-10-10
  • 打赏
  • 举报
回复
streamsize gcount() const;
试试这个方法。

64,341

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧