C++ 二进制打开并复制图像文件c++;
我想将一个图像文件复制到另一个新文件。这是我的方法:C++ 二进制打开并复制图像文件c++;,c++,file,C++,File,我想将一个图像文件复制到另一个新文件。这是我的方法: 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]; i
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();
std::of stream myOutpue;
std::ifstreammysource;
//int i=0;
open(ofn.lpstrFile,std::ios::binary);
open(“im4.jpg”,std::ios::binary);
字符缓冲区;
char bufferToSave[100];
if(mySource.is_open())
{
//client->sendFilePacket(FileStates::START\u SAVE,buffer,false,i);
i++;
而(!mySource.eof())
{
mySource>>std::noskipws>>缓冲区;
myOutpue sendFilePacket(FileStates::CONTINUE\u SAVE,buffer,false,i);
i++;
}
}
i++;
//client->sendFilePacket(FileStates::END\u SAVE,buffer,true,i);
mySource.close();
//myOutpue.close();
这个方法工作正常,但我的问题是我想复制char/bit并将其发送到另一个客户端。当我对每个字符执行此操作时,它不能正常工作,所以我想制作一个更大的buffer(例如chart[512])或类似的东西,并将它们复制到新文件中
我试着这样做:
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();
std::of stream myOutpue;
std::ifstreammysource;
open(ofn.lpstrFile,std::ios::binary);
open(“im4.jpg”,std::ios::binary);
字符缓冲区;
char bufferToSave[100];
if(mySource.is_open())
{
//client->sendFilePacket(FileStates::START\u SAVE,buffer,false,i);
i++;
而(!mySource.eof())
{
如果(i==100)
{
对于(inti=0;i<100;i++)myOutpue>std::noskipws>>缓冲区;
bufferToSave[i]=缓冲区;
//myOutpue sendFilePacket(FileStates::CONTINUE\u SAVE,buffer,false,i);
i++;
}
}
i++;
//client->sendFilePacket(FileStates::END\u SAVE,buffer,true,i);
mySource.close();
myOutpue.close();
但我得到了无法打开的图像
因此,我的问题是如何读取文件以从中获取更多位,从而创建与原始文件相同的图像。请尝试使用方法,而不是使用“手动”复制。原始文件复制算法中存在一个错误,即决不能使用
eof()
作为结束标志循环
见:
复制文件可以很简单,如下所示:
std::ofstream("output.jpg", std::ios::binary) << std::ifstream("input.jpg", std::ios::binary).rdbuf();
:
您可能希望使用iostream的read()和write()方法,而不是它的>>和>,我尝试使用以下代码:
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的数量与原始文件中的相同),所以我不知道这有什么问题?我知道已经很久了,但是读了这个主题,我找到了解决办法
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();
注意:在循环之后,您必须保存其余的读取,因为在循环中,您只保存适合缓冲区的内容,其余的您什么也不做。有时,剩余部分可以是几个字符,看起来图像大小相同,但它们不是
注2:我在这里发表文章是为了帮助那些像我一样仍处于困境的人!!这是:
而(!mySource.eof())
可能会损坏您的文件:sendFilePacket()
的参数是什么?解决了部分问题,但您希望详细说明并填写更多,以便从正确的注释到正确的答案。
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();