[ create a new paste ] login | about

Link: http://codepad.org/2GmwI7vr    [ raw code | fork ]

C, pasted on Jun 22:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void erase_multiline( char* file1, char* file2, int n1, int n2 )
{
	FILE		*fp;
	char		buff[200][80];
	int			i, max;

	// 読み込み
	fp = fopen( file1, "r" );
	for( max = 0; fgets( buff[max], 80, fp ); max++ );
	fclose( fp );

	// n1行目まで書き込み
	fp = fopen( file2, "w" );
	for( i = 0; i < n1; i++ )
	{
		fputs( buff[i], fp );
	}
	// n2+1行目~最終行までまで書き込み
	for( i = n2 + 1; i < max; i++ )
	{
		fputs( buff[i], fp );
	}
	fclose( fp );
}


void replace_lines( char* file1, char* file2, int n, int m )
{
	FILE		*fp;
	char		buff[200][80];
	char		temp[80];
	int			i, max;

	// 読み込み
	fp = fopen( file1, "r" );
	for( max = 0; fgets( buff[max], 80, fp ); max++ );
	fclose( fp );

	// 入れ替え
	for( i = 0; buff[n][i] != '\0'; i++ )
	{
		temp[i] = buff[n][i];
	}
	temp[i] = '\0';

	for( i = 0; buff[m][i] != '\0'; i++ )
	{
		buff[n][i] = buff[m][i];
	}
	buff[n][i] = '\0';

	for( i = 0; temp[i] != '\0'; i++ )
	{
		buff[m][i] = temp[i];
	}
	buff[m][i] = '\0';

	// 書き込み
	fp = fopen( file2, "w" );
	for( i = 0; i < max; i++ )
	{
		fputs( buff[i], fp );
	}
	fclose( fp );
}


void erase_string( char* file1, char* file2, char* str )
{
	FILE		*fp;
	char		buff[200][80];
	int			i, len, max;
	char		*p1, *p2, *p3;

	// 読み込み
	fp = fopen( file1, "r" );
	for( max = 0; fgets( buff[max], 80, fp ); max++ );
	fclose( fp );

	// 文字列削除
	len = strlen( str );
	for( i = 0; i < max; i++ )
	{
		p1 = p2 = buff[i];
		while( *p1 != '\0' )
		{
			p3 = strstr( p1, str );
			if( p3 )
			{
				while( p1 < p3 )
				{
					*p2++ = *p1++;
				}
				p1 += len;
			}
			else
			{
				while( *p1 != '\0' )
				{
					*p2++ = *p1++;
				}
				break;
			}
		}
		*p2 = '\0';
	}

	// 書き込み
	fp = fopen( file2, "w" );
	for( i = 0; i < max; i++ )
	{
		fputs( buff[i], fp );
	}
	fclose( fp );
}


void erase_char( char* file1, char* file2, char c )
{
	FILE		*fp;
	char		buff[200][80];
	int			i, j, k, max;

	// 読み込み
	fp = fopen( file1, "r" );
	for( max = 0; fgets( buff[max], 80, fp ); max++ );
	fclose( fp );

	// 文字削除
	for( i = 0; i < max; i++ )
	{
		for( j = 0, k = 0; buff[i][j] != '\0'; j++ )
		{
			if( buff[i][j] != c )
			{
				buff[i][k] = buff[i][j];
				k++;
			}
		}
		buff[i][k] = '\0';
	}

	// 書き込み
	fp = fopen( file2, "w" );
	for( i = 0; i < max; i++ )
	{
		fputs( buff[i], fp );
	}
	fclose( fp );
}


void replase_strings( char* file1, char* file2, char* str1, char* str2 )
{
	FILE		*fp;
	char		buff[200][80];
	char		temp[80];
	int			i, len1, len2, max;
	char		*p1, *p2, *p3;

	// 読み込み
	fp = fopen( file1, "r" );
	for( max = 0; fgets( buff[max], 80, fp ); max++ );
	fclose( fp );

	// 文字列置換
	len1 = strlen( str1 );
	len2 = strlen( str2 );
	for( i = 0; i < max; i++ )
	{
		p1 = buff[i];
		p2 = temp;
		while( *p1 != '\0' )
		{
			p3 = strstr( p1, str1 );
			if( p3 )
			{
				while( p1 < p3 )
				{
					*p2++ = *p1++;
				}
				memcpy( p2, str2, len2 );
				p1 += len1;
				p2 += len2;
			}
			else
			{
				while( *p1 != '\0' )
				{
					*p2++ = *p1++;
				}
				break;
			}
		}
		*p2 = '\0';
		strcpy( buff[i], temp );
	}

	// 書き込み
	fp = fopen( file2, "w" );
	for( i = 0; i < max; i++ )
	{
		fputs( buff[i], fp );
	}
	fclose( fp );
}


int main( void )
{
	int		n1, n2;
	int		n, m;
	char	str[ 80 ];
	char	str1[ 80 ];
	char	str2[ 80 ];
	char	c[ 2 ];

	printf( "n1=" );
	scanf( "%d", &n1 );

	printf( "n2=" );
	scanf( "%d", &n2 );

	printf( "n=" );
	scanf( "%d", &n );

	printf( "m=" );
	scanf( "%d", &m );

	printf( "string=" );
	scanf( "%s", str );

	printf( "char=" );
	scanf( "%s", c );

	printf( "string1=" );
	scanf( "%s", str1 );

	printf( "string2=" );
	scanf( "%s", str2 );

	erase_multiline( "input.txt", "erase_multiline.txt", n1 - 1, n2 - 1 );
	replace_lines( "input.txt", "replace_lines.txt", n - 1, m - 1 );
	erase_string( "input.txt", "erase_string.txt", str );
	erase_char( "input.txt", "erase_char.txt", c[ 0 ] );
	replase_strings( "input.txt", "replase_strings.txt", str1, str2 );

	return 0;
}


Create a new paste based on this one


Comments: