Raised This Month: $51 Target: $400
 12% 

Edit a word inside a text file


Post New Thread Reply   
 
Thread Tools Display Modes
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 05-24-2015 , 12:56   Re: Edit a word inside a text file
Reply With Quote #11

Btw my mind wasn't in normal state when i wrote my post and read your code.
It was making sense but there were some bugs in it. ( you fixed the one already. )

Anyways ... i created this but i quickly gave up since there was no way
to resize the file
even tho:
https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx
http://linux.die.net/man/2/truncate
And i got stuck on bug ( while what is smaller than with )
So i found that it wasn't worth of my time.
Anyways, here's my code:
Code:
#include <amxmodx>

public plugin_init()
{
	if( file_exists( "addons/logs.txt" ) ) delete_file("addons/logs.txt")
	replaceinfile( "addons/test.txt", "unab1234567asb", "3" )
}

#define _rif_block_size 32
stock replaceinfile( const filename[], const what[], const with[] )
{
	new f = fopen( filename, "rb" );
	new whatsize = strlen( what );
	new withsize = strlen( with );
	new blocksize = _rif_block_size - 1;
	new readsize[2] = {0,0};
	new mypos = 0;
	new foundin = -1;
	new myblock[2][_rif_block_size * 2];
	new b = 0;
	while((readsize[b] = fread_blocks( f, myblock[b], blocksize, BLOCK_CHAR )))
	{
		zs_log( "b:%d,%d", b, readsize[b]  )
		myblock[b][readsize[b] + 1] = 0;
		if( mypos )
		{
			add(myblock[b], readsize[b], myblock[(!b)]);
			myblock[(!b)][0] = 0;
			zs_log( "check:%s from:%s", what, myblock[b] )
			foundin = contain(myblock[b], what);
		} 
		else foundin = contain(myblock[b], what);
		
		if( foundin != -1 )
		{
			mypos += foundin;
			fclose(f);
			f = fopen( filename, "r+b" );
			fseek(f, 0, SEEK_END);
			new fsize = ftell(f);
			new current = fsize;
			new stepsize;
			readsize[0] = _rif_block_size - 1;
			if( withsize > whatsize )
			{
				mypos += whatsize;
				stepsize = withsize - whatsize;
				while( current > mypos )
				{
					if( mypos > current - readsize[0] ) readsize[0] = current - mypos;
					current -= readsize[0];
					fseek(f, current, SEEK_SET);
					fread_blocks( f, myblock[0], readsize[0], BLOCK_CHAR );
					fseek(f, current + stepsize, SEEK_SET);
					fwrite_blocks(f, myblock[0], readsize[0], BLOCK_CHAR );
				}
				fseek(f, mypos - whatsize, SEEK_SET);
				fwrite_blocks(f, with, withsize, BLOCK_CHAR );
				break;
			}
			stepsize = whatsize - withsize;
			current = mypos + whatsize;
			new pr = fsize - stepsize - 1;
			while( current < pr )
			{
				zs_log( "distance:%d/%d pr:%d", pr - current, current, pr )
				if( current + readsize[0] > pr) readsize[0] = pr - current;
				zs_log(  "writesize:%d", readsize[0])
				fseek(f, current, SEEK_SET);
				fread_blocks( f, myblock[0], readsize[0], BLOCK_CHAR );
				fseek(f, current - stepsize, SEEK_SET);
				fwrite_blocks(f, myblock[0], readsize[0], BLOCK_CHAR );
				current += readsize[0];
			}
			
			fseek(f, mypos, SEEK_SET);
			fwrite_blocks(f, with, withsize, BLOCK_CHAR );
			
			
			for( new b = 0; b <= stepsize; b++ ) myblock[0][b] = 0;
			fseek(f, fsize - stepsize, SEEK_SET);
			fwrite_blocks(f, myblock[0], stepsize, BLOCK_CHAR );
			break;
		}
		zs_log( "here" );
		b = (!b);
		myblock[b][0] = 0;
		mypos += readsize[0];
	}
	fclose(f);
}

public zs_log( in[], any:... )
{
	static a[1024]
	vformat(a, charsmax(a), in, 2)
	write_file( "addons/logs.txt", a, -1)
}
oh btw @Bugsy
that what you didn't had patience to understand.
Half of it didn't make any sense. It made sense but a bad sense i guess.

I'm not going to say what, where, i took or did but by that reason, my ability to think deep was terrible.

Sowwy about everything xD

Last edited by .Dare Devil.; 05-24-2015 at 13:01.
.Dare Devil. is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-24-2015 , 19:14   Re: Edit a word inside a text file
Reply With Quote #12

Here's a line by line flavor
PHP Code:
ReplaceTextInFile( const szFile[] , const szFind[] , const szReplaceWith[] )
{
    new 
iSrcFile iDestFile szDestFile64 ] , szLine255 ];
    
    
formatexszDestFile charsmaxszDestFile ) , "%s_tmp" szFile );
    
    if ( ( 
iSrcFile fopenszFile "r" ) ) && ( iDestFile fopenszDestFile "w+" ) ) )
    {
        while ( 
fgetsiSrcFile szLine charsmaxszLine ) ) )
        {
            
replace_allszLine charsmaxszLine ) , szFind szReplaceWith );
            
fputsiDestFile szLine );
        }
        
        
fcloseiSrcFile );
        
fcloseiDestFile );
    }
    
    return ( 
iSrcFile && iDestFile && delete_fileszFile ) && rename_fileszDestFile szFile ) );

__________________
Bugsy is offline
Old 05-25-2015, 05:52
HamletEagle
This message has been deleted by HamletEagle.
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:25.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode