我想将一个图像文件复制到另一个新文件中。这是我的方法:
std::ofstream myOutpue;
std::ifstream mySource;
//int i = 0;
mySource.open(ofn.lpstrFile, std::ios::binary);
myOutpue.open("im4.jpg", std::ios::binary);
char buffer;
char bufferToSave[100];
if (mySource.is_open())
{
//client->sendFilePacket(FileStates::START_SAVE, buffer, false,i);
i++;
while (!mySource.eof())
{
mySource >> std::noskipws >> buffer;
myOutpue << buffer;
//client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false,i);
i++;
}
}
i++;
//client->sendFilePacket(FileStates::END_SAVE, buffer, true,i);
mySource.close();
//myOutpue.close();
此方法工作正常,但我的问题是我想复制char/bit的并将其发送给另一个客户端。当我按每一个字符做这个操作时,它不能正常工作,所以我想做一个更大的buffor(例如char t512)或类似的东西,并将它们复制到新文件中。
我试着这样做:
std::ofstream myOutpue;
std::ifstream mySource;
mySource.open(ofn.lpstrFile, std::ios::binary);
myOutpue.open("im4.jpg", std::ios::binary);
char buffer;
char bufferToSave[100];
if (mySource.is_open())
{
//client->sendFilePacket(FileStates::START_SAVE, buffer, false,i);
i++;
while (!mySource.eof())
{
if (i == 100)
{
for (int i = 0; i < 100; i++)myOutpue << bufferToSave[i];
i = 0;
}
mySource >> std::noskipws >> buffer;
bufferToSave[i] = buffer;
//myOutpue << buffer;
//client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false,i);
i++;
}
}
i++;
//client->sendFilePacket(FileStates::END_SAVE, buffer, true,i);
mySource.close();
myOutpue.close();
但是我得到了我打不开的图像。
所以我的问题是如何读取文件以获得更多的位,并创建与原始图像相同的图像。
发布于 2016-02-25 01:08:59
您的原始文件复制算法中有一个错误,因为您永远不应该使用eof()
作为结束标志进行循环。
请参阅:Why is iostream::eof inside a loop condition considered wrong?
复制文件可能很简单,如下所示:
std::ofstream("output.jpg", std::ios::binary) << std::ifstream("input.jpg", std::ios::binary).rdbuf();
在传递std::istream
缓冲区(使用rdbuf()
)时,它使用输出操作符的特殊重载。它会复制整个流。
在读取整个缓冲区时,您应该使用std::istream::read
std::ifstream ifs("input.jpg", std::ios::binary)
char buffer[1025]; // create a buffer
// keep going as long as the reading succeeds
while(ifs.read(buffer, sizeof(buffer)))
{
// ifs.gcount() is the number of chars read successfully
client->sendFilePacket(buffer, ifs.gcount()); // send all bytes
}
发布于 2018-12-12 05:36:21
我知道已经有很长一段时间了,但通过阅读这些主题,我找到了解决方案:
std::ifstream ifs(ofn.lpstrFile, std::ios::binary);
std::ofstream myOutpue;
char buffer[1024]; // create a buffer
myOutpue.open("output.jpg", std::ios::binary);
//client->sendFilePacket(FileStates::START_SAVE, buffer, false, i);
while (ifs.read(buffer, sizeof(buffer)))
{
myOutpue.write(buffer, ifs.gcount());
}
//
myOutpue.write(buffer, ifs.gcount());
myOutpue.close();
注意:我的答案类似于@dawcza94,但为了避免黑屏,在循环之后,你必须保存剩余的读数,因为在循环中,你只保存适合缓冲区的内容,而忽略其余的。有时,其他图像可能只有几个字符,看起来图像大小相同,但事实并非如此。
Note2:我在这里发帖是为了帮助那些仍然像我一样陷入困境的人!
发布于 2016-02-25 06:35:53
我尝试使用以下代码:
std::ifstream ifs(ofn.lpstrFile, std::ios::binary);
std::ofstream myOutpue;
char buffer[1024]; // create a buffer
myOutpue.open("output.jpg", std::ios::binary);
//client->sendFilePacket(FileStates::START_SAVE, buffer, false, i);
while (ifs.read(buffer, sizeof(buffer)))
{
//client->sendFilePacket(FileStates::CONTINUE_SAVE, buffer, false, ifs.gcount());
myOutpue.write(buffer, ifs.gcount());
}
//client->sendFilePacket(FileStates::END_SAVE, buffer, true, i);
myOutpue.close();
但当我这样做时,在我的图像副本中,我只得到了原始图像的一半和黑屏的一半( kb的数量与原始文件中的相同),所以我不知道这有什么问题?
https://stackoverflow.com/questions/35607986
复制相似问题