作爲一個加密項目的一部分,我爲了獲得樂趣,我想將文件的內容(PDF,DOCX,JPEG,任何ASCII文件)與內容字符數字化,然後在最後顛倒過程以給出原始文件類型的文件,其可以像常規那樣打開等。C++正確複製文件的內容
首先,我測試的時候以爲我會將一個.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.
}
這是什麼問題以及如何程序進行編輯,以提供有效的文件?
提前歡呼。
呃,謝謝,它現在有效。 – user1546083