[英]C++ Exactly copying the contents of a file
作為我正在制作的加密項目的一部分,我想獲取文件的內容,無論是PDF,DOCX,JPEG,任何ASCII文件,以數字方式使用字符,然后在最后反轉該過程提供原始文件類型的文件,可以正常打開等。
首先,當我測試時,我認為我會將.docx文件的內容讀入字符串,然后將其直接寫入另一個具有不同名稱的.docx文件。 但是,完成此操作后,Microsoft Word將拒絕打開該文件:“文件已損壞,無法打開”。
這是我用來復制文件的代碼:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputFile("C:\\Users\\MyUser\\Documents\\This is a test.docx", ios::in | ios::binary);
inputFile.seekg(0, ios::end);
int length = inputFile.tellg();
inputFile.seekg(0, ios::beg);
string fileContents;
fileContents.resize(length);
inputFile.read(&fileContents[0], length);
inputFile.close();
ofstream outputFile("C:\\Users\\MyUser\\Documents\\TestCopy.docx", ios::app);
outputFile.write(&fileContents[0], length);
outputFile.close();
cout << "Complete.";
int n;
cin >> n; //Keeps the program open so message can be read.
}
問題是什么以及如何編輯程序以提供有效文件?
提前干杯。
您的輸出流不是二進制模式。
看起來您忘了為輸出文件(ios :: binary)設置二進制標志。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.