Edit a word inside a text file
I've been struggling for the past hour, but with no use. I just can't understand how files work in amxx. Maybe because I'm tired.
Anyways, suppose we have a file inside amxmodx/configs/messages.txt and the contents of messages.txt are PHP Code:
How would I do this? I tried reading the whole file as a string then using replace_all and I failed miserably. Code snips would also be very welcome |
Re: Edit a word inside a text file
Read the file. Change the appropriate words. Write to the file (make sure to overwrite original contents).
Quote:
Quote:
|
Re: Edit a word inside a text file
You can take a look inside BombStatus plugin, it has the feature to remove specific line from file. You can adapt that and work with it.
If you are too lazy: PHP Code:
|
Re: Edit a word inside a text file
here's how I tried to do it. @Hamlet, I need to change a word not a whole line, but I will make it work with your way
It's only called once so no need to cache stuff PHP Code:
|
Re: Edit a word inside a text file
Then:
PHP Code:
|
Re: Edit a word inside a text file
I get how it works now. You're reading line by line, then checking the lines, then writing line by line on a new file.
Isn't it possible to read the entire file into memory then rewrite the original file? (asking out of curiosity, this method is just fine) |
Re: Edit a word inside a text file
Have you tried LoadFileForMe()?
|
Re: Edit a word inside a text file
This will replace every instance of szFind with szReplaceWith in a file. Keep in mind that this reads the file in DATA_BUFFER_SIZE chunks and if szFind is located between chunks then it will not get replaced. Easiest way to avoid this is to size DATA_BUFFER_SIZE larger than the file, or use a method that reads 1 line at a time. If you're using 1.8.3+, use replace_string() instead of replace_all().
Edit: Updated it to handle szReplaceWith[] that is larger than szFind[]. If szReplaceWith is larger, it will use half of the data buffer for reading file data, and the other half to allow for the extra characters in szReplaceWith. If szReplaceWith is <= szFind, it will use the full buffer for reading file data. If OP can give more details about the potential size of this file, I can probably tweak it to be more reliable. If all else fails, a line-by-line replacer may be better. PHP Code:
PHP Code:
|
Re: Edit a word inside a text file
Quote:
Quote:
|
Re: Edit a word inside a text file
thanks for posting guys, but I used ham's implementation with reading line by line
|
All times are GMT -4. The time now is 19:37. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.