如何在C中的文本文件中查找和提取特定单词? [英] How to find and take specific word from text file in C?

查看:61
本文介绍了如何在C中的文本文件中查找和提取特定单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该函数很大,但是我从该函数中获取了一些代码,并且有一个问题,即如何从文本文件中找到该特定名称?我不知道.

  for(选项= 1;选项< = sizeof(列表)/sizeof(字符*); ++选项)printf(%d.您的%s:\ n",选项,列出[选项-1]);fputs(选择您要选择的更新:",stdout);if((scanf(%d",& option)!= 1)||(option< 1)||(option> sizeof(listing)/sizeof(char *))){puts(无效的选择");fclose(fr3);返回-1;}int one_by_one;printf(输入新名称以更新:");scanf(%s",& updated_name);用于(one_by_one = 0; file_text [one_by_one]!='\ 0'; one_by_one ++){while(file_text [one_by_one] == Updated_name){one_by_one ++;如果(one_by_one == strlen(updated_name)){找到= 1;printf(发现:\ n");休息;}}} 

解决方案

如果尝试搜索和更新任何名称,则应使用 fgets()从源文件中提取一行,并将其存储在一个临时的 char 数组.然后,您可以使用字符串函数(如 strstr()函数)在该行中查找名称,如果找到该名称,则替换为 continue .

您可以引用此

If you are trying to search and update any name, you should take a line from source file using fgets() and store in a temporary char array. Then you can use string function like strstr() function to find the name in that line, if found replace it otherwise continue.

You can refer to this link for some help.

这篇关于如何在C中的文本文件中查找和提取特定单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆