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

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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?