fovgrubby 2015-08-28 04:47 采纳率: 0%
浏览 4039
已结题

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放到一个数组里。

  • 写回答

3条回答 默认 最新

  • qq1223386926 2015-08-28 07:06
    关注
    #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\n", argv[ 0 ] );
                    return -1;
            }
    
            iRet = fetch_string( argv[ 1 ], &stpResult );
            if( iRet == 0 )
            {
                    struct st_result *stpTmp = stpResult;
    
                    while( stpTmp != NULL )
                    {
                            printf( "str: %s\n", 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\n" );
                    return -1;
            }
    
            while(  fgets( caCurLine, 512, fp ) != NULL )
            {
                    struct st_result *stpTmp = NULL;
                    char   *cpTmp = NULL;
    
                    if( caCurLine[ strlen( caCurLine ) - 1 ] == '\n' )
                            caCurLine[ strlen( caCurLine ) - 1 ] = '\0';
    
                    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\n" );
                            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 ] == '\n' )
                            caCurLine[ strlen( caCurLine ) - 1 ] = '\0';
    
                    /* 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\n" );
                                    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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀