C++ 将多个文件合并为一个文件
好的,我想把多个文件连接成一个。问题是最终文件是空的。我想知道如何将内容写入文件而不被覆盖等等。这是我的密码。 提前谢谢C++ 将多个文件合并为一个文件,c++,file,concatenation,C++,File,Concatenation,好的,我想把多个文件连接成一个。问题是最终文件是空的。我想知道如何将内容写入文件而不被覆盖等等。这是我的密码。 提前谢谢 void concatenate() { fstream fileToConcatenate, result; unsigned numberOfFiles = 0; char fileName[MAX], finalFileName[MAX]; puts("Please tell me how many files you want
void concatenate()
{
fstream fileToConcatenate, result;
unsigned numberOfFiles = 0;
char fileName[MAX], finalFileName[MAX];
puts("Please tell me how many files you want to concatenate.");
cin >> numberOfFiles;
puts("Please tell me data for the resulted file.\n*Hint: full path followed by the file name.\n*E.g:C:\\Users\\IoanaAlexandra\\test.txt");
cin >> finalFileName;
result.open(finalFileName, ios::out|ios::ate);
for (unsigned i = 0; i < numberOfFiles; i++)
{
switch (i)
{
case 0:puts("Please tell me the file data for the first file to be concatenated.\n*Hint: full path followed by the file name.\n*E.g:C:\\Users\\IoanaAlexandra\\test.txt"); break;
case 1:puts("Please tell me the file data for the second file."); break;
case 2:puts("Please tell me the file data for the third file."); break;
default:cout << "Please tell me the file data for the " << i << "th file."; break;
}
cin >> fileName;
fileToConcatenate.open(fileName, ios::in);
if (result.is_open())
{
if (fileToConcatenate.is_open())
{
result << fileToConcatenate.rdbuf();
}
else
{
puts("The file you are trying to concatenate from doesn't exist!Try again!");
concatenate();
}
}
else
{
puts("The result file could not be created! Try again!");
concatenate();
}
}
fileToConcatenate.close();
result.close();
}
void concatenate()
{
fstream fileToConcatenate,结果;
unsigned numberOfFiles=0;
char fileName[MAX],finalFileName[MAX];
puts(“请告诉我您要连接多少个文件。”);
cin>>文件数量;
puts(“请告诉我结果文件的数据。\n*提示:完整路径后跟文件名。\n*例如:C:\\Users\\IoanaAlexandra\\test.txt”);
cin>>最终文件名;
结果.open(finalFileName,ios::out | ios::ate);
for(无符号i=0;i 默认设置:cout从循环外部删除结果。open()
,然后尝试将代码更改为如下内容:
fileToConcatenate.open(fileName, ios::in);
if (fileToConcatenate.is_open())
{
// open output file
result.open(finalFileName, ios::out|ios::ate);
// you should check so the output file really was opened correctly here
result << fileToConcatenate.rdbuf();
// close the input file
fileToConcatenate.close();
// close output file
result.close();
}
else
{
puts("The file you are trying to concatenate from doesn't exist!Try again!");
concatenate();
}
fileToConcatenate.open(文件名,ios::in);
if(fileToConcatenate.is_open())
{
//打开输出文件
结果.open(finalFileName,ios::out | ios::ate);
//您应该进行检查,以便在此处正确打开输出文件
结果抱歉,我不太知道如何执行xd。哦,我明白了。您正在编辑。不必担心,在循环中再次调用concatenate()
之前,您是否应该关闭fileToConcatenate
?程序的输出是什么?只是第一个文件的内容。