📄 trimfile.c
字号:
#include <stdio.h>
#include <string.h>
#include "../../young/youngc.h"
void loop()
{
char* pos;
ncstring line, infile, outfile, symbol;
FILE* infp = NULL;
FILE* outfp = NULL;
ncstr_init( &infile );
ncstr_init( &outfile );
ncstr_init( &symbol );
ncstr_init( &line );
while( 1 )
{
printf( "\nplease input source file path and name, no input to exit:\n" );
ncstr_getline( &infile, '\n' );
if( ncstr_size(&infile) == 0 )
goto EXITLOOP;
printf( "\nplease input destination file path and name:\n" );
ncstr_getline( &outfile, '\n' );
if( eq_ncstring(&infile, &outfile) == 0 )
{
printf( "\ninput error: source file = destination file!\n" );
goto EXITLOOP;
}
printf( "\nplease input symbol:\n" );
ncstr_getline( &symbol, '\n' );
infp = fopen( ncstr_to_string(&infile), "r" );
outfp = fopen( ncstr_to_string(&outfile), "w" );
if( !infp || !outfp )
printf( "file open error!\n" );
else
{
while( !feof(infp) )
{
ncstr_fgetline( &line, infp, '\n' );
pos = strstr( ncstr_to_string(&line), ncstr_to_string(&symbol) );
if( pos )
{
ncstr_erase_range( &line, pos - ncstr_begin(&line), SIZE_MAX );
if( ncstr_size(&line) == 0 )
continue;
}
fprintf( outfp, "%s\n", ncstr_to_string(&line) );
}
printf( "%s to %s OK!\n", ncstr_to_string(&infile),
ncstr_to_string(&outfile) );
}
if( infp )
fclose( infp );
if( outfp )
fclose( outfp );
}
EXITLOOP:
ncstr_destroy( &infile );
ncstr_destroy( &outfile );
ncstr_destroy( &symbol );
ncstr_destroy( &line );
}
void sequence( int argc, char* argv[] )
{
int i;
char* pos;
ncstring line, outfile, symbol;
FILE* infp = NULL;
FILE* outfp = NULL;
ncstr_init( &outfile );
ncstr_init( &symbol );
ncstr_init( &line );
ncstr_format( &symbol, "%s", argv[argc - 1] );
for( i = 1; i < argc - 1; ++i )
{
infp = fopen( argv[i], "r" );
ncstr_format( &outfile, "%s_", argv[i] );
outfp = fopen( ncstr_to_string(&outfile), "w" );
if( !infp || !outfp )
printf( "\nfile open error!\n" );
else
{
while( !feof(infp) )
{
ncstr_fgetline( &line, infp, '\n' );
pos = strstr( ncstr_to_string(&line), ncstr_to_string(&symbol) );
if( pos )
{
ncstr_erase_range( &line, pos - ncstr_begin(&line), SIZE_MAX );
if( ncstr_size(&line) == 0 )
continue;
}
fprintf( outfp, "%s\n", ncstr_to_string(&line) );
}
printf( "%s to %s OK!\n", argv[i], ncstr_to_string(&outfile) );
}
if( infp )
fclose( infp );
if( outfp )
fclose( outfp );
}
ncstr_destroy( &outfile );
ncstr_destroy( &symbol );
ncstr_destroy( &line );
}
int main( int argc, char* argv[] )
{
if( argc < 3 )
loop();
else
sequence( argc, argv );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -