#include <iostream>
#include <ctime>
using namespace std;
// f1: 1 2 3 4 5 6 7
// f2: 10 20 30 40
// f3: 1 10 2 20 3 30 4 40 5 6 7
void infile(FILE *f, char *s)
{
int dat;
int n=rand()%10+1;
f=fopen(s,"wb");
for (int i=0;i<n;i++)
{
dat=rand()%10;
fwrite(&dat,sizeof(int),1,f);
}
fclose(f);
}
void outfile(FILE *f, char *s)
{
int dat;
f=fopen(s,"rb");
while (fread(&dat,sizeof(int),1,f))
{
cout<<dat<<" ";
}
cout<<endl;
fclose(f);
}
void from_f1f2_tof3(FILE *f1, char *s1, FILE *f2, char *s2, FILE *f3, char *s3)
{
int dat1, dat2;
bool ff1,ff2;
f1=fopen(s1,"rb");
f2=fopen(s2,"rb");
f3=fopen(s3,"wb");
do
{
ff1=fread(&dat1,sizeof(int),1,f1);
ff2=fread(&dat2,sizeof(int),1,f2);
if (ff1)
{
fwrite(&dat1,sizeof(int),1,f3);
}
if (ff2)
{
fwrite(&dat2,sizeof(int),1,f3);
}
}
while ((ff1) || (ff2));
fclose(f1);
fclose(f2);
fclose(f3);
}
int main()
{
setlocale(LC_ALL,"rus_rus.1251");
srand(time(0));
char s1[80]="d:\\1.txt";
char s2[80]="d:\\2.txt";
char s3[80]="d:\\3.txt";
FILE *f1, *f2, *f3;
infile(f1,s1);
cout<<"file1: "<<endl;
outfile(f1,s1);
infile(f2,s2);
cout<<"file2: "<<endl;;
outfile(f2,s2);
from_f1f2_tof3(f1,s1,f2,s2,f3,s3);
cout<<"file3: "<<endl;
outfile(f3,s3);
system("pause");
return 0;
}
|