为什么不能合并文件?
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define SIZE 50
char *input_sData(char *ps,int n);
void output_sData(char *ps,int n);
void WriteFile(char *ps,char *sName,int n);
void catFile(char *srcName,char *dstName);
int main(int argc,char *argv[])
{
char buffer[SIZE];
int len;
input_sData(buffer,SIZE);
len = strlen(buffer);
WriteFile(buffer,"mike.dat",len);
input_sData(buffer,SIZE);
len = strlen(buffer);
WriteFile(buffer,"mike1.dat",len);
catFile("mike.dat","mike1.dat");
return 0;
}
/*
** 输入数据
*/
char *input_sData(char *ps,int n)
{
int i;
int ch;
i = 0;
while((i < n - 1) && (ch = getchar()) != EOF && ch != '\n')
ps[i++] = ch;
ps[i] = '\0';
return ps;
}
/*
** 写文件
*/
void WriteFile(char *ps,char *sName,int n)
{
FILE *fp = NULL;
if((fp = fopen(sName,"w")) == NULL)
{
printf("Can't open file %s\n",sName);
exit(1);
}
else
{
fwrite(ps,sizeof(char),n,fp);
fclose(fp);
}
}
/*
** 合并文件
*/
void catFile(char *srcName,char *dstName)
{
FILE *fps = NULL;
FILE *fpd = NULL;
int ch;
if((fps = fopen(srcName,"r")) == NULL)
{
printf("Can't open file %s\n",srcName);
exit(1);
}
else
{
fseek(fps,1,SEEK_END);
}
if((fpd = fopen(dstName,"r")) == NULL)
{
printf("Can't open file %s\n",dstName);
exit(1);
}
while( (ch = fgetc(fpd)) != EOF)
fputc(ch,fps);
fclose(fps);
fclose(fpd);
}
文件不能合并。
例: mike.dat 内容:123
mike1.dat 内容: abc
catFile("mike.dat","mike1.dat") 之后,文件不能合并?
怎么回事呢?