急!C语言 FILES 问题

来源:百度知道 编辑:UC知道 时间:2024/06/23 12:12:46
输入了 一个序号比如 A12
然后 在某个TXT 给定的文件中 读去文件中的内容 寻找到所有“A12”开头的各行 如果存在则打印所有“A12”开头的行 不存在则打印出“NO LINE”(A12 如果存在 则一定是所在行开头)

谢谢!

#include <stdio.h>

void main( void )
{
FILE *stream;
char line[100];
char sub_str[] = "A12";
char *p_dest = NULL;
int cnt = 0;

if( (stream = fopen( "fgets.c", "r" )) != NULL )
{
while( !feof( stream ) )
{
if( fgets( line, 100, stream ) == NULL)
{
printf( "fgets error\n" );
break;
}
else
{
p_dest = strstr(line,sub_str);
if( p_dest-line==0 )
{
cnt++
printf( "%s", line);
p_dest = NULL;
}
}
}

fclose( stream );
}
else
{
printf("open file failed\n");
}

if( cnt == 0 )
{
print