Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中查找文本文件中的字符串?_C_String_File_Text - Fatal编程技术网

如何在C中查找文本文件中的字符串?

如何在C中查找文本文件中的字符串?,c,string,file,text,C,String,File,Text,我需要读取文本文件中的字符串序列并从中提取信息。该文件包含游戏角色的名称和ID。我需要为每个英雄取其各自的英雄名(“url”标记),然后将其存储到一个链接列表中,但在处理文本文件时,我在C语法方面遇到了很多困难。具体来说,我不知道如何搜索英雄,获取各自的号码和url,并将其存储到链接列表中 以下是我唯一能够编写的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> typedef stru

我需要读取文本文件中的字符串序列并从中提取信息。该文件包含游戏角色的名称和ID。我需要为每个英雄取其各自的英雄名(“url”标记),然后将其存储到一个链接列表中,但在处理文本文件时,我在C语法方面遇到了很多困难。具体来说,我不知道如何搜索英雄,获取各自的号码和url,并将其存储到链接列表中

以下是我唯一能够编写的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_hero{
    char name[30];
    int id;
    char attr[3];
    struct s_hero* next;
} type_hero;

type_hero* initialize(void){
    return NULL;
}

int main(){
    type_hero* hero = initialize();
    FILE *fp = fopen("npc_heroes.txt", "rt");

    return 0;
}
#包括
#包括
#包括
类型定义结构s_hero{
字符名[30];
int-id;
char-attr[3];
结构s_hero*下一步;
}英雄型;
键入英雄*初始化(无效){
返回NULL;
}
int main(){
键入_hero*hero=initialize();
文件*fp=fopen(“npc_heros.txt”、“rt”);
返回0;
}
这是我必须阅读的文本文件:

\include
#包括
#包括
类型定义结构s_hero{
字符名[30];
int-id;
char-attr[3];
结构s_hero*下一步;
}英雄型;
键入英雄*初始化(无效){
返回NULL;
}
字符*strip_dq(字符*str){
字符*从,*到;
for(from=to=str;*from;++from){
如果(*从!=“”)
*to++=*from;
}
*to='\0';
返回str;
}
键入英雄*新英雄(字符*名称,整数id){
类型_hero*hero=calloc(1,sizeof(*hero));
strcpy(英雄->姓名,姓名);
英雄->身份=身份;
回归英雄;
}
无效打印列表(键入英雄*顶部){
while(顶部){
printf(“%d:%s\n”,顶部->id,顶部->名称);
顶部=顶部->下一步;
}
printf(“\n”);
}
无效下拉列表(键入英雄*顶部){
如果(顶部){
下拉列表(顶部->下一步);
免费(顶部);
}
}
int main(){
键入_hero*hero=new_hero(“,0);//dummy
类型_hero*curr=hero;
文件*fp=fopen(“npc_heros.txt”、“rt”);
字符buff[128];
而(1==fscanf(fp,“%127s”,buff)){
如果(strcmp(buff,“\”英雄主义“)==0){//label
fscanf(fp,“%s”,buff);//数据
int id=atoi(条带dq(buff));
而(1==fscanf(fp,“%127s”,buff)和&strcmp(buff,“\'url\”)!=0)
;//跳过
fscanf(fp,“%s”,buff);//数据
//字符名[30];
//strcpy(名称、条纹(浅黄色));
curr=curr->next=new\u-hero(脱衣舞步(buff),id);
}
}
咖喱=英雄;
英雄=当前->下一步;
自由(当前);//放置虚拟对象
打印列表(英雄);
下拉列表(英雄);
返回0;
}

这个问题在堆栈溢出中已经有了答案:@SujithKarivelil,虽然所谓的dup确实演示了如何在文件中查找字符串,但这个问题提出的解析问题可能更复杂;并且可能没有通过给出的答案完全解决。我认为您的回答不正确。我可以在sin中转换文本文件吗找到字符串,然后使用字符串函数进行搜索?”@RodrigoronConrichter Yep,您可以使用
fread()
将文件数据读入缓冲区,然后使用
strstr()
查找第一个出现的字符串
strstr
函数将返回指向字符串第一次出现的开头的指针。技术人员的名称(
“url”
)不存在。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct s_hero {
    char name[30];
    int id;
    char attr[3];
    struct s_hero* next;
} type_hero;

type_hero* initialize(void){
    return NULL;
}

char *strip_dq(char *str){
    char *from, *to;

    for(from = to = str; *from ; ++from){
        if(*from != '"')
            *to++ = *from;
    }
    *to = '\0';
    return str;
}

type_hero *new_hero(char *name, int id){
    type_hero *hero = calloc(1, sizeof(*hero));
    strcpy(hero->name, name);
    hero->id = id;
    return hero;
}

void print_list(type_hero *top){
    while(top){
        printf("%d:%s\n", top->id, top->name);
        top = top->next;
    }
    printf("\n");
}

void drop_list(type_hero *top){
    if(top){
        drop_list(top->next);
        free(top);
    }
}

int main(){
    type_hero *hero = new_hero("", 0);//dummy
    type_hero *curr = hero;
    FILE *fp = fopen("npc_heroes.txt", "rt");
    char buff[128];
    while(1==fscanf(fp, "%127s", buff)){
        if(strcmp(buff, "\"HeroID\"")==0){//label
            fscanf(fp, "%s", buff);//data
            int id = atoi(strip_dq(buff));
            while(1==fscanf(fp, "%127s", buff) && strcmp(buff, "\"url\"")!=0)
                ;//skip
            fscanf(fp, "%s", buff);//data
            //char name[30];
            //strcpy(name, strip_dq(buff));
            curr = curr->next = new_hero(strip_dq(buff), id);
        }
    }
    curr = hero;
    hero = curr->next;
    free(curr);//drop dummy

    print_list(hero);
    drop_list(hero);
    return 0;
}