C语言提取源文件?

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>

#define N 1000

int main(void)

{

FILE *in, *out

int i

in = fopen("源.c", "rb")

char a[N]

fread(a, sizeof(char), N, in)

for (i = 0 i < N i++)

{

if (a[i] == '/'&&a[i + 1] == '/'&&a[i + 2] == 's'&&a[i + 3] == 't'&&a[i + 4] == 'o'&&a[i + 5] == 'p'&&a[i + 粗局6] == '0'&&a[i + 7] == '0'&岩慧让&a[i + 8] == '1')

{

(a[i + 9] = '\0')

break

}

}

printf("%s\n",a)

out = fopen("程序.txt", "wb")

fwrite(a, sizeof(char), N, out)

return 0

}//stop001

输出结果

第一次编译需要注释或删除掉那个for循环,第二次好使,这个源文件叫“源.c”,碧派可以根据程序大小改define,最后的停止标志stop001也可以改


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12418172.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存