按你的要求改了一下。这段代码能把键盘输入的特定字符串后面双引号内的文字提取出来存入XYZ.txt文件中。在提取过程中滤除了源文件中的'\n'和' '。供参考……
#include "stdio.h"//
#include "string.h"//
#include "stdlib.h"//
void main(void){
FILE *pR,*pW;
char CoStr[20],ln,ch,i=0;
if(!(pR=fopen("ABC.txt","r"))){
printf("源文件打开失败...");
exit(0);
}
printf("请输入特定字符串...\nCoStr=");
ln=strlen(gets(CoStr));
do{
fseek(pR,-i,SEEK_CUR);
for(i=0;i
if((ch=getc(pR))!=CoStr[i]) break;
if(ch==EOF){
printf("没有发现特定字符串%s!\n",CoStr);
exit(0);
}
}while(i
if(!(pW=fopen("XYZ.txt","w"))){
printf("创建目标文件失败...");
exit(0);
}
while((ch=getc(pR))!='"' && ch!=(char)176);
while((ch=getc(pR))!='"' && ch!=(char)177){
if(ch=='\n' || ch==' ') continue;
putc(ch,pW);
}
fclose(pW);
fclose(pR);
printf("文件已成功建立,名为XYZ.txt\n");
}
取消
评论