0
我寫了一個代碼,我嘗試讀取一個bmp文件並將其寫入另一個文件。當我嘗試寫入輸出文件時,它會創建但不會打開。這裏是我的代碼讀取一個bmp文件數據並將其寫入另一個bmp
#include<iostream>
#include<fstream>
using namespace std;
//int writeFile(string content);
int main() {
ifstream myReadFile;
ofstream myWriteFile;
myReadFile.open("D:/MIT_Database/barbara_gray.bmp");
myWriteFile.open("D:/MIT_Database/barbara_graywrite.bmp");
char output[100];
string content;
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
getline(myReadFile, content);
cout<<content;
// myReadFile >> output;
for(int i=0;i<content.length();i++)
{myWriteFile<<content[i];}
// myWriteFile<<content<<'\n';
myWriteFile<<'\n';
}
}
myReadFile.close();
myWriteFile.close();
return 0;
}
由於您在Windows上,您可能需要使用[CopyFile](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v = vs.85).aspx)功能由系統提供。 –
你怎麼知道輸出文件無法打開?你似乎沒有檢查。 – aschepler
當時訪問文件的一個字節很慢。如果'CopyFile'不是一個選項,那麼請參閱https://stackoverflow.com/a/10195497/4603670這個答案來複制C++中的二進制文件。 –