文本查找-C语言从txt文件查找字符串求助

问题描述

C语言从txt文件查找字符串求助

txt文件内容如下:

A->B
hello everybody haha hehe
B->A
world is big
A->B
impossible is nothing
B->A
just do it
需求:
首先查找字符串hello,找到后提取其上一行字符串A->B,然后依次查找下一个A->B或者B->A(两个都可以),提取出其下一行第一个字符串,也就是world ,impossible ,just 然后把hello,world,impossible,just放到一个数组里。

解决方案

C语言 读取文件 查找字符串
【C语言】查找字符串
C语言字符串查找函数

解决方案二:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct st_result{
                char caStr[ 100 ];
                struct st_result *next;
        };

int fetch_string( char *cpPath, struct st_result **stpResult);
int clear_result( struct  st_result *stpResult);

int
main( int argc, char *argv[] )
{
        struct st_result *stpResult = NULL;
        int  iRet = 0;

        if( argc != 2 )
        {
                fprintf( stderr, "usage: %s file
", argv[ 0 ] );
                return -1;
        }

        iRet = fetch_string( argv[ 1 ], &stpResult );
        if( iRet == 0 )
        {
                struct st_result *stpTmp = stpResult;

                while( stpTmp != NULL )
                {
                        printf( "str: %s
", stpTmp->caStr );
                        stpTmp = stpTmp->next;
                }
        }

        clear_result( stpResult );

        return iRet;
}

int
fetch_string( char *cpPath, struct st_result **stpResult )
{
        FILE *fp = NULL;
        char caPreLine[ 512 ];
        char caCurLine[ 512];
        char caPreStd[ 128 ];
        char caEndStd[ 128 ];
        char caStrStd1[ 512 ];
        char caStrStd2[ 512 ];

        if( ( fp = fopen( cpPath, "r" ) ) == NULL )
        {
                fprintf( stderr, "fopen error
" );
                return -1;
        }

        while(  fgets( caCurLine, 512, fp ) != NULL )
        {
                struct st_result *stpTmp = NULL;
                char   *cpTmp = NULL;

                if( caCurLine[ strlen( caCurLine ) - 1 ] == '
' )
                        caCurLine[ strlen( caCurLine ) - 1 ] = '';

                if( strstr( caCurLine, "hello" ) == NULL )
                {   /* not find the 'hello' */
                        strcpy( caPreLine, caCurLine );
                        continue;
                }
                /* find the hello */
                if( ( stpTmp = malloc( sizeof( struct st_result ) ) ) == NULL )
                {
                        fprintf( stderr, "malloc error
" );
                        return -1;
                }

                stpTmp->next = *stpResult;
                *stpResult = stpTmp;
                sscanf( caCurLine, "%s", stpTmp->caStr );

                sscanf( caPreLine, "%[^-]", caPreStd );
                if( ( cpTmp = strstr( caPreLine, "->" ) ) != NULL )
                {
                        strcpy( caEndStd, cpTmp + strlen( "->" ) );
                }
                strcpy( caStrStd1, caPreLine );
                sprintf( caStrStd2, "%s->%s", caEndStd, caPreStd );

                break;
        }

        memset( caPreLine, 0x0, sizeof( caPreLine ) );
        while( fgets( caCurLine, 512, fp ) != NULL )
        {
                struct st_result *stpTmp = NULL;

                if( caCurLine[ strlen( caCurLine ) - 1 ] == '
' )
                        caCurLine[ strlen( caCurLine ) - 1 ] = '';

                /* if previous line is A->B or B->A */
                if( strstr( caPreLine, caStrStd1 ) != NULL || strstr( caPreLine, caStrStd2 ) != NULL )
                {
                        if( ( stpTmp = malloc( sizeof( struct st_result ) ) ) == NULL )
                        {
                                clear_result( *stpResult );
                                fprintf( stderr, "malloc error
" );
                                return -1;
                        }

                        stpTmp->next = *stpResult;
                        *stpResult = stpTmp;

                        sscanf( caCurLine, "%s", stpTmp->caStr );
                        strcpy( caPreLine, caCurLine );
                }
                else
                        strcpy( caPreLine, caCurLine );
        }
        return 0;
}

int
clear_result( struct  st_result *stpResult)
{
        while( stpResult != NULL )
        {
                struct st_result *stpTmp = stpResult;

                stpResult = stpResult->next;
                free( stpTmp );
        }

        return 0;
}

解决方案三:

读取这个txt文件,根据'
'换行符判断是否换行取字符串,根据''空格键判断第一个字符串的截取位置。逻辑很清晰,用C写不难。

解决方案四:

为什么要首先查找hello呢?
直接查找A->B或者B->A不是同样的效果吗?
这样代码实现起来简单的多。

时间: 2024-09-19 09:59:45

文本查找-C语言从txt文件查找字符串求助的相关文章

c语言空txt文件读出乱码

问题描述 c语言空txt文件读出乱码 新建的空的txt文件,读取telephone.txt到链表,显示所有信息时读出的是一堆乱码,但是txt文件里没有存放任何数据. /*读取telephone.txt文件中的内容创建学生链表*/ ID* create() { ID head; ID *p1,*p2; p2 = (ID)malloc(LENTEL); head = p2; FILE *fp; if((fp = fopen("telephone.txt","r")) =

c语言-如何用C语言实现.txt文件中内容的添加

问题描述 如何用C语言实现.txt文件中内容的添加 问题,如何用C语言中append()函数实现5.9.13行的功能:依次将1.2.3写入到D盘ccc.txt中. void main(){ ............1 int a=4,b=2: ............2 if(a*b==8) ............3 { ............4 //将1写入D盘ccc.txt文件中 ............5 } ............6 if(a-b==2) ............7

文件操作-c语言实现txt文件读取

问题描述 c语言实现txt文件读取 VC编译器下c语言没有办法打开TXT文件.代码如下:#include #includevoid main() { FILE *fp; if((fp=fopen(""C:UsersAdministratorDesktopad.txt""r""))==NULL) { printf(""cannot open the file!""); exit(0); } else prin

文件查找函数 php

函数 文件查找函数 php   CODE:   <?php /*文件查找函数用法:findfile (目录,是否遍历子目录,是否查找文件内容,不查找的目录) ;Ketle 2005-07-07*/function findfile ($dir, $find_sub_dir=false, $find_content=false, $except_dir=false) {                $d = dir($dir);        while (false !== ($entry =

如何写robots.txt 文件?

robots.txt文件是放在网站的根目录下,作用是告诉搜索引擎网站的哪些文件,哪些目录可以被索引,哪些不要索引.因此,这个文件是相当的重要,正确书写好robots.txt文件,可以保证你的网页被搜索引擎正确地索引.收录. 书写robots.txt文件时,要使用纯文本的编辑器,使文件是纯文本的格式.robots.txt文件的结构还是比较简单的.它是由一行一行的纪录组成,每行由两个部份组成:<Field> : <value> <Field> 有两种,一个是User-age

C语言中对文件最基本的读取和写入函数_C 语言

C语言read()函数:读文件函数(由已打开的文件读取数据)头文件: #include <unistd.h> 定义函数: ssize_t read(int fd, void * buf, size_t count); 函数说明:read()会把参数fd 所指的文件传送count 个字节到buf 指针所指的内存中. 若参数count 为0, 则read()不会有作用并返回0. 返回值为实际读取到的字节数, 如果返回0, 表示已到达文件尾或是无可读取的数据,此外文件读写位置会随读取到的字节移动.

C语言 格式化读写文件详解_C 语言

fscanf() 和 fprintf() 函数与前面使用的 scanf() 和 printf() 功能相似,都是格式化读写函数,两者的区别在于 fscanf() 和 fprintf() 的读写对象不是键盘和显示器,而是磁盘文件. 这两个函数的原型为: int fscanf ( FILE *fp, char * format, ... ); int fprintf ( FILE *fp, char * format, ... ); fp 为文件指针,format 为格式控制字符串,... 表示参数

界面设计-从txt文件里查找数据,然后放在界面上

问题描述 从txt文件里查找数据,然后放在界面上 界面设计和txt文件已经上传到ssoil2015@163.com 邮箱的文件中心里,密码:nft123456 点击了Load之后打开对话框,把txt文件中数据显示在界面上,数据时=后面 "!"前面的数据,和日期,点击了OK之后将修改的数据 解决方案 没看到测试数据 解决方案二: 这是经过调用txt文件之后的界面效果. 点了OK保存在txt中,和之前调用txt文件格式一样,只是某些数据变化了 解决方案三: 这是经过调用txt文件之后的界面

查找目录下的所有文件中是否含有某个字符串 linux

查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xargs grep -ri "IBM" -l 1.正则表达式    (1)正则表达式一般用来描述文本模式的特殊用法,由普通字符(例如字符a-z)以及特殊字符(称为元字符,如/.*.?等)组成.   (2)基本元字符集及其含义       ^ :只匹配行首.   如^a 匹配以a开头的行abc,