⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fgrep.c

📁 C和指针非常好的一本书.里面的有许多代码可以借鉴.
💻 C
字号:
/*
** Find and print all of the lines in the named files that
** contain the given string.
**
**	Usage:
**		fgrep string file [ file ... ]
*/

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

#define	BUFFER_SIZE	512

void
search( char *filename, FILE *stream, char *string )
{
	char	buffer[ BUFFER_SIZE ];

	while( fgets( buffer, BUFFER_SIZE, stream ) != NULL ){
		if( strstr( buffer, string ) != NULL ){
			if( filename != NULL )
				printf( "%s:", filename );
			fputs( buffer, stdout );
		}
	}
}

int
main( int ac, char **av )
{
	char	*string;

	if( ac <= 1 ){
		fprintf( stderr, "Usage: fgrep string file ...\n" );
		exit( EXIT_FAILURE );
	}

	/*
	** Get the string.
	*/
	string = *++av;

	/*
	** Process the files.
	*/
	if( ac <= 2 )
		search( NULL, stdin, string );
	else {
		while( *++av != NULL ){
			FILE	*stream;

			stream = fopen( *av, "r" );
			if( stream == NULL )
				perror( *av );
			else {
				search( *av, stream, string );
				fclose( stream );
			}
		}
	}

	return EXIT_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -