1、设A.txt和B.txt都在当前目录下,读打开A.txt和B.txt,写打开C.txt。按字符循环读取A.txt文件一行并逐字符写入C.txt文件,但当读到'\n'时不写入\n'而启动另一内循环按字符读取B.txt文件的一行并逐字符写入C.txt文件,当写入'\n'后跳出内循环停止读取B.txt文件转而再去读A.txt文件的下一行……如此反复,直到遇到A.txt文件的EOF时结束。
2、代码如下:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "stdlib.h"
int main(void){
FILE *fpa,*fpb,*fpc;
char ch;
fpa=fopen("A.txt","r");
fpb=fopen("B.txt","r");
fpc=fopen("C.txt","w");
if(!fpa || !fpb || !fpc){
printf("Open the file(s) failure...\n");
exit(0);
}
while((ch=fgetc(fpa))!=EOF){
if(ch=='\n')
while(fputc(ch=fgetc(fpb),fpc),ch!='\n' && ch!=EOF);
else fputc(ch,fpc);
}
fclose(fpc);
fclose(fpb);
fclose(fpa);
return 0;
}
这个很简单 晚上回去我帮你写
请记得在E盘下准备
新建文本文档1.txt
新建文本文档2.txt
#include
void main()
{
char* file1Path = "e:/新建文本文档1.txt";
char* file2Path = "e:/新建文本文档2.txt";
char* filePath = "e:/结果.txt";
FILE* fp1 = fopen(file1Path, "r");
FILE* fp2 = fopen(file2Path, "r");
if(fp1==NULL)
{
printf("%s不存在", file1Path);
return;
}
if(fp2==NULL)
{
printf("%s不存在", file2Path);
fclose(fp1);
return;
}
FILE* fp = fopen(filePath, "w");
char buf[512] = {0};
int count;
while(!feof(fp1))
{
count = fread(buf, sizeof(char), 512, fp1);
fwrite(buf, sizeof(char), count, fp);
}
while(!feof(fp2))
{
count = fread(buf, sizeof(char), 512, fp2);
fwrite(buf, sizeof(char), count, fp);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
}
#include
main(){
system("copy file06_1.txt + file06_2.txt myfile.txt");
printf("the merged text is in myfile.txt\n");
}