Using Files to write data/values(old and new file commands)
1 Attachment(s)
This is a little tutorial about reading/writing files to store something.
This Tutorial need basics in Amxx Scripting. I am not so good in explain and writing Tutorials,so the most are writing as code. I dont explain every Command. If there something wrong or better please post. Lets begin with the old file commands (easy and short). Code:
It returns index of next line or 0 when the end of the file is reached. The number of characters read is stored in txtLen. Code:
Code:
The next are the new file commands(many and very powerfull) Lets begin with basics of new file commands and write/read a string inside a file. This is good for textfiles where easily can read the file The new file commands dont read linespecific,they use a cursor inside the file to read the data. This cursor position can get with Code:
Code:
start can be: Code:
SEEK_SET //beginning of the file Code:
Code:
It returns a pointer to the file. Code:
"file" must be a pointer to a file from the fopen command. Code:
fputs/fprint dont add a newline at end of the string,so it must set manually. Code:
Code:
To write a file where must save many data ,the plugin growing big and are many work to write with using Parse,str_to_,... Here a little trick that i have learn to use strucs in Pawn. This is very easy to manage the filedata and adding new data,but every change need to change/convert also the file. Code:
With "new variablename[enumname]" it create a variable with the arraylenght of the given enum. Code:
Mode can be: Code:
BLOCK_INT //Good to write any value/char Code:
Code:
I hope i can help some with this little tutorial. At the end i add the final Plugin that can be used as core to write/read data from file. |
Re: Using Files to write data/values(old and new file commands)
Nice tutorial.
Anyway, I have a question for you. I'm writing a plugin that tries to update some lines in files with new text... I was trying to just use appending to the file, but it seems like whenever append is used, it adds a new line... let me try to explain what I'm trying to do a bit better: There is a string (containing spaces) of an unknown length saved to a text file. I would like to allow the user to type in a phrase (not of a limited length), such as "likes blue cars." so that I can add it to the unknown string on the unknown string's line. So it would read in the unknown, which might be "Bad Bud ", add in the new string "likes blue cars." with format, so the final string would read "Bad Bud likes blue cars.", and save it back to its original line in the text file (overwriting "Bad Bud " with "Bad Bud likes blue cars."). How can I do this? I've tried, but it keeps adding weird characters to the end of the added string. They look like this: o>‘|b>‘| Any suggestions? Sorry for crappily explaining my problem. |
Re: Using Files to write data/values(old and new file commands)
Both of the strings are unknown. One is saved from previous user input, and the new one is saved from user input, all I need to do is combine them. It doesn't really matter what the values are, I just used those phrases as examples for what the user might have typed in. Is it not possible to just read a line until you reach the end of the line then stop? What I was doing was pseudo working, because it was reading the text from the file, but it was adding those weird end of string and backslash characters and I'm not sure how to get rid of them.
If you want to see my code as an example, I can send it to you. I just don't want everyone getting access to it because it's something new and cool and needs more stuff to make it work in its entirety. |
Re: Using Files to write data/values(old and new file commands)
I was trying to use the old file system for reading specific lines, because that's how they're written to the file. I'll send you the pm. Each time it reads a line it seems to bring in that other junk at the end.
|
Re: Using Files to write data/values(old and new file commands)
yeah,I'll try this
thx |
Re: Using Files to write data/values(old and new file commands)
Hmm... I had a small bug. It no longer prints those random characters anymore. I'll still send you the code when I'm done, if you're interested.
|
Re: Using Files to write data/values(old and new file commands)
Sorry, it's still kinda buggy, so I'm not done yet. So many exceptions I need to take care of to make it work...
|
Re: Using Files to write data/values(old and new file commands)
Here some info you might want to add.. When using fgets it will read the full line including the new line marker.. ^n
http://wiki.amxmodx.org/index.php/AM...t_File_Natives |
Re: Using Files to write data/values(old and new file commands)
I have a question. Im saving origins to a file , And I want to load them all into a menu how would i do this with the new read wrtie commands?
|
Re: Using Files to write data/values(old and new file commands)
Based on your plugin in another help thread i have make the example for it.
WARNING:This example read ONLY 1 line,if you want read more you need a for or while loop. I have make 2 plugins,how you can use it,but i prior the second. I have add some things and have comments some out,which can be removed or uncomment. Both plugins are tested and work,but are not optimized. Code:
The second way: Code:
|
All times are GMT -4. The time now is 23:50. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.