主要目的是读取eingabe.txt文件,根据条件创建文件ausgabe.txt
当遇到有这样一行出现时:#EINBAU datei1.txt
在这里插入datei1.txt里面的全部内容,其他行则直接复制eingabe.txt的内容到ausgabe.txt。
我自己写的代码如下,但是不能完成任务,已经研究了很久,也不知道是哪里出了问题,望高手指教!
--------------------------------------------------------------------------
#include <string.h>
#include <stdio.h>
void writeto(FILE *source,FILE *ziel){
char ch;
ch=fgetc(source);
while(ch!=EOF){
fputc(ch,ziel);
ch=fgetc(source);
}
}
int main(void)
{
char *p;
FILE *fpr,*fpi,*fpn;
char str[81],a[2][80];
int i = 0;
if((fpr=fopen("d:\\eingabe.txt","at+"))==NULL){
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
if((fpn=fopen("d:\\ausgabe.txt","wt"))==NULL){
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
while(feof(fpr)==0){
fgets(str,80,fpr);
p = strtok(str, " ");
if (p) strcpy(a[0],p);
p = strtok(NULL, " ");
if (p) strcpy(a[1],p);
if (a[0] == "#EINBAU\n"){
if((fpi=fopen("a[1]","rt"))==NULL){
printf("Cannot open file strike1 any key exit!");
getch();
exit(1);
}
writeto(fpi,fpn);
}
else
fputs(str,fpn);
}
fclose(fpr);
fclose(fpi);
fclose(fpn);
}
------------------------------------------------------------------------
eingabe.txt
------------------------------------------------------------------------
abcdefg hijklmn opq rst
uvw xyz
123 321
456
#EINBAU d:\\datei1.txt
7890 1234 567
345 678
1234567
ab cd efg
------------------------------------------------------------------------
datei1.txt
------------------------------------------------------------------------
qwertyu asdfghj xcvbn
swer werwe erer ewq
qwewq wqeqw rtrgf
fsdf sdfsdf
------------------------------------------------------------------------
[此贴子已经被作者于2006-5-25 8:20:29编辑过]