#include <stdio.h>
#include <stdlib.h>
#define FILE1 "file06_1.txt"
#define FILE2 "file06_2.txt"
#define MYFILE "myfile.txt"
#define MAXLINE 1024
void ReadFile(char *filename)
{
FILE *fp;
int n;
char buf[MAXLINE];
if ((fp = fopen(filename, "r+")) == NULL)
{
printf("open %s error!", filename);
return;
}
while(n = fread(buf,1, MAXLINE-1, fp))
{
buf[n] = 0;
printf("%s", buf);
}
fclose(fp);
}
void CreateFile(char *file1, char *file2, char *filename)
{
FILE *fp, *fpt;
char buf[MAXLINE];
int n;
if ((fp = fopen(filename, "w+")) == NULL)
{
printf("open %s error!", filename);
return;
}
if ((fpt = fopen(file1, "r")) == NULL)
{
printf("open %s error!", file1);
fclose(fp);
return;
}
while(n = fread(buf,1, MAXLINE, fpt))
{
fwrite(buf, 1, n, fp);
}
fclose(fpt);
if ((fpt = fopen(file2, "r")) == NULL)
{
printf("open %s error!", file2);
return;
}
fwrite("\n", 1, 1, fp);
while(n = fread(buf,1, MAXLINE, fpt))
{
fwrite(buf, 1, n, fp);
}
fclose(fpt);
fclose(fp);
}
void Proc(void)
{
char c;
c = getchar();
switch(c)
{
case '1':
ReadFile(FILE1);
break;
case '2':
ReadFile(FILE2);
break;
case '3':
CreateFile(FILE1, FILE2, MYFILE);
ReadFile(MYFILE);
break;
case '4':
exit(0);
default:
printf("Undefine!\n");
}
}
int main(void)
{
while (1)
{
system("cls");
printf("1.读取第一部分并显示\n");
printf("2.读取第二部分并显示\n");
printf("3.生成新文件\n");
printf("4.退出\n");
Proc();
getchar();
while(getchar() != '\n');
}
}