C语言提取源文件?

Python020

C语言提取源文件?,第1张

这段代码能把键盘输入的特定字符串后面双引号内的文字提取出来存入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=0i<lni++)

if((ch=getc(pR))!=CoStr[i]) break

if(ch==EOF){

printf("没有发现特定字符串%s!\n",CoStr)

exit(0)

}

}while(i<ln)

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")

}

你的单词是中文还是英文,中文应该是不可以。。

英文测试无误

#include

<stdio.h>

#include

<string.h>

#define

PATH

"word.txt"

FILE

*fp

int

i

=

0

char

word[1024],str[1024]

int

main()

{

fp

=

fopen(PATH,"r")

if

(fp

==

NULL)

{

/*

code

*/

printf("error\n")

}

else

{

while((fgets(&str[i],100,fp)

!=

NULL))

{

printf("%s\n",str

)

i++

}

printf("%c",str[0])

}

}

----------------------------------

1、文件打开方式用r,我这显示rw+是错的..没弄明白

2、PATH路径应该得用\\吧,我怕麻烦直接相对路径,将word.txt放在工程路径下

----------------

调试无误。。。