导航
  • 主页
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

如何合并两个文件?

hzhstar 2004-04-08 09:18:06
这两个文件都是自定义扩展名的,比如a.trn,b.trn,(trn自己指定)
两个文件的内容是在程序中写入的,
而且两个文件是按位写入的,如:
a文件:
for(x=1;x<100;x++)
{
int sum=0;
for (i=0; i<10; ++i, x= x>>1, sum = sum<<1)
{ if (x & 1) sum |= 1;
fputc(sum,fp);
}
}

b文件:
for(x=100;x<200;x++)
{int sum=0;
for (i=0; i<10; ++i, x= x>>1, sum = sum<<1)
{ if (x & 1) sum |= 1;
fputc(sum,fp);
}
}




...全文
83 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
itmaster 2004-04-09
给你个源代码:
http://www.csdn.net/cnshare/soft/6/6235.shtm
回复
hzhhy 2004-04-09
谢谢:wangjs720(听雨舟)大哥,
我这两个文件的难度在于是按位写入的,
按位的读出应该和我们一般文件不一样吧,
您给的这程序可以实现按位读入吗?

我是想把第二个文件接到第一个文件中去,
呵呵~~~~~~~~~

回复
xuanzg 2004-04-09

在程序里执行dos 命令
copy a.trn+b.trn a.trn /y
回复
社会栋梁 2004-04-09
void CFSDlg::Merge()//文件分割器的合并功能
{
CFile SourceFile,TargetFile;
CString m_NoPathFileName,m_FileTitle,m_FilePath;
CString DriveLetter,SourceFileName,SourceFileExName;
CString TargetFileName;
CFileStatus status;
char Buff[1024];
char *BuffPointer;
char NumberString[5];
int nFileTitleLength;
int nBlockCounter,StringLength;
int FileKBytes,CopiedKBytes;
int MessageBoxReturn;
long int FileLength,CopiedLength;
UINT BuffLength,nReadLength;

//Open FSI file
if (SourceFile.Open(m_FileName,
CFile::modeRead|CFile::typeBinary)
==FALSE)
{
MessageBox("Can't open source file.",
"File Split",
MB_ICONINFORMATION|MB_OK);
return;
}
m_NoPathFileName=SourceFile.GetFileName();
nFileTitleLength=m_NoPathFileName.ReverseFind('.');
if (nFileTitleLength!=-1)
m_FileTitle=m_NoPathFileName.Left(nFileTitleLength);
else
m_FileTitle=m_NoPathFileName;
m_FilePath=m_FileName.Left
(m_FileName.GetLength()-m_NoPathFileName.GetLength());

//Verify FSI file
SourceFile.Read(Buff,1024);
if((Buff[0]!='F')||(Buff[1]!='S')||(Buff[2]!='I'))
{
MessageBox("Incorrect file format.",
"File Split",
MB_ICONINFORMATION|MB_OK);
return;
}

//Get file head information
BuffPointer=Buff;
for(int i=1;i<=3;i++)
BuffPointer++;
TargetFileName=m_FilePath+BuffPointer;
StringLength=GetStringLength(BuffPointer);
for(i=1;i<=StringLength;i++)
BuffPointer++;
FileLength=atoi(BuffPointer);
FileKBytes=FileLength/1024;
BuffLength=1021-StringLength;
StringLength=GetStringLength(BuffPointer);
for(i=1;i<=StringLength;i++)
BuffPointer++;
BuffLength-=StringLength;

//Create target file
DriveLetter=m_FileName;
DriveLetter=DriveLetter.Left(2);
DriveLetter.MakeUpper();
if(CFile::GetStatus(TargetFileName,status))
{
if (status.m_attribute&0x01)
{
MessageBoxReturn=MessageBox
((CString)"Over write read only file "+TargetFileName+'?',
"File Split",
MB_ICONQUESTION|MB_YESNOCANCEL);
if(MessageBoxReturn==IDYES)
{
status.m_attribute=0x00;
CFile::SetStatus(TargetFileName,status);
}
}
else
MessageBoxReturn=MessageBox
((CString)"Over write "+TargetFileName+'?',
"File Split",
MB_ICONQUESTION|MB_YESNOCANCEL);
if(MessageBoxReturn==IDCANCEL)
{
SourceFile.Close();
return;
}
}
if((DriveLetter.Compare("A:")==0)||(MessageBoxReturn==IDNO))
{
CFileDialog SaveFile(FALSE,NULL,TargetFileName,
OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,
"All files (*.*)|*.*||");
if (SaveFile.DoModal()==IDOK)
{
TargetFileName=SaveFile.GetPathName();
if (CFile::GetStatus(TargetFileName,status))
{
status.m_attribute=0x00;
CFile::SetStatus(TargetFileName,status);
}
}
else
{
SourceFile.Close();
return;
}
}
if (TargetFile.Open(TargetFileName,
CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary)
==FALSE)
{
MessageBox("Can't create target file.",
"File Split",
MB_ICONINFORMATION|MB_OK);
SourceFile.Close();
return;
}

//Restore target file
CopiedLength=0;
nBlockCounter=0;
CopiedKBytes=0;
while(TRUE)
{
TargetFile.Write(BuffPointer,BuffLength);
BuffPointer=Buff;
CopiedLength+=BuffLength;
CopiedKBytes++;
m_Progress.SetPos((int)100*CopiedKBytes/FileKBytes);
if (FileLength<=CopiedLength)
break;

//Read from block
if ((nReadLength=SourceFile.Read(Buff,1024))==0)
{
SourceFile.Close();

//Generate next file name
nBlockCounter++;
itoa(nBlockCounter,NumberString,10);
SourceFileExName=(CString)"00"+NumberString;
SourceFileExName=SourceFileExName.Right(3);
SourceFileName=m_FilePath+m_FileTitle+'.'+SourceFileExName;

//Open next block
if (SourceFile.Open(SourceFileName,
CFile::modeRead|CFile::typeBinary)
==FALSE)
{
if (DriveLetter.Compare("A:")==0){
do
{
if(MessageBox("Insert next disk("+SourceFileName+").", "File Split",MB_ICONINFORMATION|MB_OKCANCEL)
==IDCANCEL){
MessageBox("Can't open block Files.","File Split",MB_ICONINFORMATION|MB_OK);
TargetFile.Close();
return;
}
}
while(SourceFile.Open(SourceFileName,
CFile::modeRead|CFile::typeBinary)
==FALSE);
}
else
{
MessageBox("Can't open block files.",
"File Split",
MB_ICONINFORMATION|MB_OK);
TargetFile.Close();
return;
}
}

//Read from new block
if ((nReadLength=SourceFile.Read(Buff,1024))==0)
{
MessageBox("Can't read from block files.",
"File Split",
MB_ICONINFORMATION|MB_OK);
TargetFile.Close();
return;
}
}
BuffLength=nReadLength;
}

//Verify target file
nReadLength=SourceFile.Read(Buff,1024);
SourceFile.Close();
if (((TargetFile.GetLength())!=((UINT)FileLength))
||(nReadLength!=0))
{
MessageBox("Unexpected error!",
"File Split",
MB_ICONINFORMATION|MB_OK);
TargetFile.Close();
return;
}

TargetFile.Close();
return;
}
回复
yxlijinzhu 2004-04-09
你可以先在将这两个文件的数据读入到内存中, 然后在内存中合并这两个文件, 因为, 操作系统中对文件管理的最小单位是族, 所以你得先在内存中合并, 再将结果写入文件中。
回复
wangjs720 2004-04-08
// App_Test2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include <sys/stat.h>
#include <iostream.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0


unsigned int temp = 0;
size_t size = 0;

bool BindFiles(char *first,char *next,char *outfilename)
{
FILE* pFile1;
FILE* pFileOut;
FILE* pFile2;
char *buf;
int ReadSize = 0;
int TotalSize = 0;
struct _stat st;
unsigned int temp = 0x12345678;
unsigned int i, k;
int l=1;
char buff[20];
int sync;
char *part;

_stat(first, &st);
size = st.st_size;
if (size == 0)
{
cout<<"自身文件长度为零时出错!"<<endl;
return false;
}

buf = (char *)malloc(size);
if (buf == NULL)
{
cout<<"内存空间不足!"<<endl;
return false;
}

pFile1 = fopen(first, "rb");
if (pFile1 == NULL)
{
free(buf);
cout<<"打开文件错误!"<<endl;
return false;
}

ReadSize = fread(buf, 1, size, pFile1);
fclose(pFile1);

if (ReadSize != (int)size)
{
free(buf);
cout<<"读取文件错误!"<<endl;
return false;
}

for (i = 0; i < size - sizeof(temp); i += sizeof(temp))
{
for (k = 0; k < sizeof(temp); k++)
{
if (buf[i+k] != ((char *)&temp)[k])
break;
}
}

if (_stat(first, &st) != 0 || st.st_size == 0)
{
free(buf);
cout<<"读取文件错误!"<<endl;
return false;
}

pFileOut = fopen(outfilename, "wb");
if (pFileOut == NULL)
{
free(buf);
cout<<"创建合成文件失败!"<<endl;
return false;
}

TotalSize += fwrite(buf, 1, ReadSize, pFileOut);

pFile2 = fopen(first, "rb");
if (pFile2 == NULL)
{
free(buf);
cout<<"打开文件错误!"<<endl;
return false;
}

TotalSize += fwrite(&st.st_size, 1, sizeof(st.st_size), pFileOut);

TotalSize += fwrite(&sync, 1, sizeof(int), pFileOut);

while (ReadSize = fread(buf, 1, size, pFile2))
{
TotalSize += fwrite(buf, 1, ReadSize, pFileOut);
}
fclose(pFile2);

part = _ltoa(l, buff, 10);
strcat(part,"个文件已绑定");
l++;

pFile2 = fopen(next, "rb");
if (pFile2 == NULL)
{
free(buf);
cout<<"打开文件错误!"<<endl;
return false;
}
while (ReadSize = fread(buf, 1, size, pFile2))
{
TotalSize += fwrite(buf, 1, ReadSize, pFileOut);
}

part = _ltoa(l, buff, 10);
strcat(part,"个文件已绑定");
l++;

fclose(pFile2);
fclose(pFileOut);
free(buf);

return true;
}

int main(int argc, char* argv[])
{
char f1[256] = {0};
char f2[256] = {0};
char f3[256] = {0};
int index = 0;
cout<<"输入待合并的第一个文件(完整路径):"<<endl;
cin>>f1;
cout<<"输入待合并的第二个文件(完整路径):"<<endl;
cin>>f2;
cout<<"输入合并后的文件(完整路径):"<<endl;
cin>>f3;
cout<<"你可以执行以下操作:"<<endl;
cout<<"\t开始合并请按1\n\t退出程序请按2"<<endl;
do
{
cin>>index;
if(index != 1 && index != 2)
{
cout<<"无效操作"<<endl;
continue;
}
}while(index != 1 && index != 2);
if(index == 1)
{
if(!BindFiles(f1,f2,f3))
cout<<"合并失败"<<endl;
else
cout<<"合并完成"<<endl;
}
else
exit(0);
return 0;
}
回复
快乐鹦鹉 2004-04-08
将两个文件都打开,一个文件从头读,一个文件先Seek到尾,然后,一个fgetc,一个fputc。
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……