用C语言编写文本检索程序

用C语言编写文本检索程序,第1张

#include"stdio.h"

#include"stdlib.h"

main()

{

FILE *fp

char str[80],*p,*head

int n=0

long filelen

loop1: printf("请输入您要用于 *** 作的文件名及其完整路径:\n")

gets(str)

fp=fopen(str,"rb+")

if(fp==NULL)

{

printf("找不到指定文件,要指定新文件吗?(y=是的,要指定新文件\telse=不用了,退出程序)\t")

gets(str)

if((char)tolower((int)str[0])=='y')goto loop1

else exit(0)

}

printf("请输入您要查找并替换的字符(如果输入的字符数量大于1本程序会自动舍去多余的部分)\n")

gets(str)

printf("请输入您要将找到的字符或字符串替换为的字符(自动舍去多余部分)\n")

gets(&str[1])

fseek(fp,0L,2)

filelen=ftell(fp)

fseek(fp,0L,0)

head=(char *)malloc(filelen)

fread(head,filelen,1,fp)

for(p=headp<head+filelen/1Lp++)if(*p==str[0]){*p=str[1]n++}

fseek(fp,0L,0)

fwrite(head,filelen,1,fp)

fclose(fp)

printf("替换已经完成,共替换%d处\n",n)

}

关键问题是用逗号分隔的字符串,不能用逗号格式这样分开地读出来。要用"%[^,],%[^,],%f"格式才行。另外读参数用&temp[n].mark才行,你漏了&.--------你先用下面程序读2行,01010101,ABABCEVVZ,96.0020202,EFGWE,56.0试验成功再读全部。#include#includestructstudent{charno[9]charname[10]floatmark}typedefstructstudentSTUSTU*tempintmain(){intnFILE*fp=fopen("student.txt","r")temp=(STU*)malloc(100*sizeof(STU))for(n=0n<2n++){fscanf(fp,"%[^,],%[^,],%f",temp[n].no,temp[n].name,&temp[n].mark)printf("%s%s%f\n",temp[n].no,temp[n].name,temp[n].mark)}fclose(fp)}--读完的控制用if(fscanf()==EOF)break

#include<stdio.h>

main()

{

    char *ptr, filename[50]/*定义变量及数组为字符型*/

    printf("please input the file name you want to search:\n")

    scanf("%s", filename)/*输入要查找的文件名*/

    if ((ptr = searchpath(filename)) != NULL)

        printf("the path is:%s\n", ptr)/*将文件所在的路径输出*/

    else

        printf("cannot find")/*若文件未找到,输出双引号内提示信息*/

}


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

原文地址:https://54852.com/yw/7783003.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存