问题描述
- 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不是同样的效果吗?
这样代码实现起来简单的多。
时间: 2025-01-29 19:22:00