如何在C ++中拆分文件格式的文件 [英] How to split a file with file format in C++
问题描述
我一直在寻找使用C ++分割任何文件格式的文件的解决方案。到目前为止我发现并尝试过,只能拆分.txt文件。可以将(例如.docx)文件拆分为2个文件,但打开拆分文件会出错。其他文件格式也是如此。例如,尝试下面的代码使用不同的文件扩展名而不是.txt或.dat(比如.jpg)。
是否有人可以帮助我?< br $>
最好的问候,
我尝试过的事情:
I've been looking for a solution to split a file with any file format, using C++. what i've found so far and tried as well, could only split .txt file. To split (say .docx) file into 2 files is possible, but to open splitted files give error. The same is true with other file formats. For example try the the code below with a different file extension rather than .txt or .dat (say .jpg).
is anyone who could help me please?
Best Regards,
What I have tried:
<pre>#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
ifstream fin1, fin2;
ofstream fout;
char ch, file_name1[20], file_name2[20], file_name3[30];
cout<<"\n Enter First File Name with Extension '.txt' : ";
gets(file_name1);
cout<<"\n Enter Second File Name with Extension '.txt' : ";
gets(file_name2);
cout<<"\n Enter Third File Name with Extension '.txt' ";
cout<<"\n (which will Store the Contents of \n First File and Second File) : ";
gets(file_name3);
fin1.open(file_name1);
fin2.open(file_name2);
if(fin1==NULL || fin2==NULL)
{
cout<<"\n Invalid File Name. \n There is no such File or Directory ...";
exit(EXIT_FAILURE);
}
fout.open(file_name3);
if(!fout)
{
cout<<"\n Invalid File Name. \n There is no such File or Directory ...";
exit(EXIT_FAILURE);
}
while(fin1.eof()==0)
{
fin1>>ch;
fout<<ch;
}
while(fin2.eof()==0)
{
fin2>>ch;
fout<<ch;
}
cout<<"\n Two Files have been Merged into "<<file_name3<<" File Successfully...!!!";
fin1.close();
fin2.close();
fout.close();
return 0;
}
推荐答案
我一直在寻找一种解决方案,使用C ++分割任何文件格式的文件。
I've been looking for a solution to split a file with any file format, using C++.
没有。
整个概念都是错误的。您可以获取.docx文件并将其拆分为2个文件,但生成的文件将不是有效的.docx文件。
原因很简单docx是指msword文档文件格式,它意味着该文件尊重格式,这意味着该文件有一个结构,通过拆分文件,你得到2个结构破碎的文件,这使它们不可读。
There is none.
The whole concept is wrong. You can get a .docx file and split it in 2 files, but the resulting files will not be valid .docx files.
The reason is simple docx refers to "msword document file format", it means that the file respects a format, it means that the file have a structure, by splitting the file, you get 2 files with broken structure, and it makes them unreadable.
你不能拆分文件使用格式化数据以简单的方式。如果你想这样做,你必须将所有数据加载到你的应用程序的数据模型中,并且 THAN 决定将哪些数据保存在一个或另一个文件中。
示例:压缩图像文件,首先加载它(解压缩数据),然后保存上半部分(压缩),然后保存下半部分(压缩)。
You cant split files with formatted data in a simple manner. If you want to do it, you must load all data into a data model of your app and THAN decide which data to save in one or another file.
Example: compressed image file, first load it (uncompress the data), than save the upper half (with compression) and than save the lower half (with compression).
这篇关于如何在C ++中拆分文件格式的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!