テキストファイルから指定した文字列を含む行を取出して表示する |
|
|
#include <stdio.h>
#include <memory.h>
void
main( int argc, char *argv[] )
{
FILE *fp;
fp = fopen( argv[1], "rt" );
if ( fp == NULL ) {
exit( 1 );
}
char buffer[4096];
int line;
int len;
int i;
int TargetLen;
TargetLen = strlen( argv[2] );
line = 0;
while( 1 ) {
fgets( buffer, 4096, fp );
if ( feof( fp ) ) {
break;
}
line++;
len = strlen( buffer );
for( i = 0; i < len; i++ ) {
if ( memcmp( buffer+i, argv[2], TargetLen ) == 0 ) {
printf( "%d : %s", line, buffer );
break;
}
}
}
fclose( fp );
}
| |
|
|
|
|
#include <stdio.h>
#include <memory.h>
void
main( int argc, char *argv[] )
{
FILE *fp;
fp = fopen( argv[1], "rt" );
if ( fp == NULL ) {
exit( 1 );
}
char buffer[4096];
char buffer2[4096];
int line;
int len;
int i,j;
int TargetLen;
int ReplaceLen;
TargetLen = strlen( argv[2] );
ReplaceLen = strlen( argv[3] );
line = 0;
while( 1 ) {
fgets( buffer, 4096, fp );
if ( feof( fp ) ) {
break;
}
memset( buffer2, 0x00, 4096 );
line++;
len = strlen( buffer );
j = 0;
for( i = 0; i < len; i++ ) {
if ( memcmp( buffer+i, argv[2], TargetLen ) == 0 ) {
strcat( buffer2, argv[3] );
j = j + ReplaceLen;
i = i + TargetLen - 1;
}
else {
buffer2[j] = buffer[i];
j++;
}
}
printf( "%s", buffer2 );
}
fclose( fp );
}
| |
|
|
|