gpt4 book ai didi

c++ - 如何从 IStream 读取到一个 char 数组并将其写入另一个 IStream?

转载 作者:可可西里 更新时间:2023-11-01 09:48:11 25 4
gpt4 key购买 nike

我试图通过套接字连接发送图像,但我遇到了以下代码的问题:

//stream to char array
STATSTG myStreamStats;
ULONG bytesSaved;
myStream->Stat(&myStreamStats, 0);
char* streamData = new char[myStreamStats.cbSize.QuadPart];
if(myStream->Read(streamData, myStreamStats.cbSize.QuadPart, &bytesSaved) == S_OK)
cout<<"OK!"<<endl;
else
cout<<"Not OK!"<<endl;

//char array to stream
if(myStreamR->Write(streamData, myStreamStats.cbSize.QuadPart, &bytesSaved) == S_OK)
cout<<"OK!"<<endl;
else
cout<<"Not OK!"<<endl;

//saving the image to a file
myImage = Image::FromStream(myStreamR);
myImage->Save(lpszFilename, &imageCLSID, NULL);

程序编译并运行,但我没有得到我的图像。如果我使用原始的“myStream”而不是使用从原始流读取的 char 数组构造的“myStreamR”,我会得到它。

输出是两个“OK!”,这意味着所有字节都被复制到数组中,并且所有字节都被粘贴到新流中。但是,我检查了 saveBytes,发现在 read() 之后它的值为 0(不好),而在 write() 之后它等于我给出的流大小。那么,如果没有读取任何内容,为什么 read() 会给我一个“S_OK”标志?

最佳答案

在向 MyStreamR 写入数据后,您并不是要回到开头。 Image::FromStream() 从流的当前位置开始读取,因此如果您不向后查找,将没有数据可供读取。

试试这个:

STATSTG myStreamStats = {0};
if (FAILED(myStream->Stat(&myStreamStats, 0)))
cout << "Stat failed!" << endl;
else
{
char* streamData = new char[myStreamStats.cbSize.QuadPart];

ULONG bytesSaved = 0;
if (FAILED(myStream->Read(streamData, myStreamStats.cbSize.QuadPart, &bytesSaved)))
cout << "Read failed!" << endl;
else
{
//char array to stream
if (FAILED(myStreamR->Write(streamData, bytesSaved, &bytesSaved)))
cout << "Write failed!" << endl;
else
{
LARGE_INTEGER li;
li.QuadPart = 0;

if (FAILED(myStreamR->Seek(li, STREAM_SEEK_SET, NULL)))
cout << "Seek failed!" << endl;
else
{
//saving the image to a file
myImage = Image::FromStream(myStreamR);
if (myImage1->GetLastStatus() != Ok)
cout << "FromStream failed!" << endl;
else
{
if (myImage->Save(lpszFilename, &imageCLSID, NULL) != Ok)
cout << "Save failed!" << endl;
else
cout << "OK!" << endl;
}
}
}
}
}

关于c++ - 如何从 IStream 读取到一个 char 数组并将其写入另一个 IStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13715049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com