如何解决这个文件的重复追加问题?
下面的这个程序是我想将文件456的内容追加到123里面去,但是每编译一次就追加一次,不是我要的目的,我只想它追加一次就行了,怎样解决呢?
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main(void)
{
FILE *in ,*out;
char str[100];
in = fopen("f:\\123.txt", "a");
out = fopen("f:\\456.txt", "r");
if(in == NULL)
{
printf("Error in opening the file.\n");
exit(1);
}
fprintf(in, "%s", "\n");
if(out == NULL)
{
printf("Can not open the file.\n");
}
while(fgets(str, 100, out) != NULL)
{
fprintf(in, "%s", str);
}
fclose(out);
fclose(in);
in = fopen("f:\\123.txt", "r");
while(fgets(str, 100, in) != NULL)
{
fputs(str, stdout);
}
fclose(in);
return 0;
}
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main(void)
{
FILE *in ,*out,*mark=NULL;
//////////////////////////
////////假设你第一次合并文件的时候这个标志文件不存在,
////////这个标志文件只是为了用来判断,如果你想再次合并文件,只需要删除这个标志文件
////////一样的道理如果你不想合并文件,你可以手动创建这个标志文件。
////////只要这个标志文件存在,就假设你已经合并过123.txt和456.txt
mark=fopen("f:\\make.txt", "r");
if(mark)
{
printf("Additional file have already done.\n");
exit(1);
}
//////////////////////////////
char str[100];
in = fopen("f:\\123.txt", "a");
out = fopen("f:\\456.txt", "r");
if(in == NULL)
{
printf("Error in opening the file.\n");
exit(1);
}
fprintf(in, "%s", "\n");
if(out == NULL)
{
printf("Can not open the file.\n");
}
while(fgets(str, 100, out) != NULL)
{
fprintf(in, "%s", str);
}
fclose(out);
fclose(in);
in = fopen("f:\\123.txt", "r");
while(fgets(str, 100, in) != NULL)
{
fputs(str, stdout);
}
fclose(in);
//////////////////////////////
////////////合并完成,马上创建标志文件
mark=fopen("f:\\make.txt", "w");
if(mark == NULL)
{
printf("Failed to create file.\n");
}
fclose(mark);
///////////////////////////
return 0;
}