)). Look here :
Code:
#include <amxmodx>
#include <amxmisc>
#include <file>
#define PLUGIN "Filehandle"
#define VERSION "1.0"
#define AUTHOR "Administrator"
new filename[256]
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR)
get_configsdir(filename,charsmax(filename))
format(filename,charsmax(filename),"%s/plugins.ini",filename)
//register_concmd("amx_filetestwrite","filewritetest")
//register_concmd("amx_filetestread","filereadtest")
}
public plugin_cfg()
{
// READ MOD
/*open file in read-mode*/
new filepointer = fopen(filename,"r")
/*check if file is open,on an error filepointer is 0*/
if(filepointer)
{
new readdata[128],plugin[32]
new parsedplugin[32]
// MY METHOD
//The new file commands dont read linespecific,they use a cursor inside the file to read the data.
//This cursor position can get with
ftell(filepointer) // not filename ??
//And the cursor can set with
fseek ( filepointer, 1, SEEK_SET ) // beginning of the file , but position?? i can set 1 ? =]
//move the cursor position to begin of the file.
//rewind ( filepointer ) NOT WORK, UNDEFINED SYMBOL
//Open a file with given filename and mode.
//It returns a pointer to the file.
fopen ( filename,"a+" )
//Write a formated string to a text file.Same as the format/formatex command,but for files.
//fputs/fprint dont add a newline at end of the string,so it must set manually.
//fputs ( filename, "plugin.amxx debug") NOT GOOD?
//Write a string to a text file at current filecursor position.
//"file" must be a pointer to a file from the fopen command.
fprintf ( filepointer, "plugins.amxx debug" )
// END OF MY METHOD
/*Read the file until it is at end of file*/
/*fgets - Reads a line from a text file -- includes newline!*/
while(fgets(filepointer,readdata,charsmax(readdata)))
{
parse(readdata,parsedplugin,charsmax(parsedplugin))
if(equal(plugin,parsedplugin))
{
// MY MESSAGE HERE
break
//...
}
}
fclose(filepointer)
}
// END READ MOD
// WRITE MOD
new writedata[128]
/*fopen - Returns a file handle for advanced file functions.
Mode uses the standard C library of mode types.
The first character can be:
"a" - append
"r" - read
"w" - write
The second character can be:
"t" - text
"b" - binary
You can also append a "+" to specify both read and write.
Open a file in append+read+write mode
On mode "a+"/"w+" it create a file if there no exist
Mode "w+" overwrite a file if one exist*/
new filepointerX = fopen(filename,"a+")
/*check if file is open,on an error filepointer is 0*/
if(filepointerX)
{
/*It give 2 ways to write a string inside a file*/
/*First way*/
formatex(writedata,charsmax(writedata),"plugin.amxx debug^n")
//The new file commands need to create a newline manual with "^n"
/*write the string to the file*/
/*another commands are:
fputc - Writes a char (1 byte) to a file handle.
fputf - Writes a float (4 bytes, 8 on AMD64) to a file handle.
fputi - Writes an integer (4 bytes) to a file handle.
fputl - Writes a long (4 bytes) to a file handle. */
fputs(filepointerX,writedata)
/*Second way*/ // LIKE THIS
/*fprintf - Writes a line to a file */
fprintf(filepointerX,"plugin.amxx debug^n")
/*close the file,this is optional,but better is it to close the file*/
fclose(filepointerX)
}
// END WRTIE MOD
}
public filewritetest(id){
new writedata[128]
new steamid[32],name[32],anyvalue,Float:anyfloatvalue
get_user_authid(id,steamid,31)
get_user_name(id,name,31)
anyvalue = 10
anyfloatvalue = 5.5
/*fopen - Returns a file handle for advanced file functions.
Mode uses the standard C library of mode types.
The first character can be:
"a" - append
"r" - read
"w" - write
The second character can be:
"t" - text
"b" - binary
You can also append a "+" to specify both read and write.
Open a file in append+read+write mode
On mode "a+"/"w+" it create a file if there no exist
Mode "w+" overwrite a file if one exist*/
new filepointer = fopen(filename,"a+")
/*check if file is open,on an error filepointer is 0*/
if(filepointer)
{
/*It give 2 ways to write a string inside a file*/
/*First way*/
formatex(writedata,127,"%s %s %d %f^n",steamid,name,anyvalue,anyfloatvalue)
//The new file commands need to create a newline manual with "^n"
/*write the string to the file*/
/*another commands are:
fputc - Writes a char (1 byte) to a file handle.
fputf - Writes a float (4 bytes, 8 on AMD64) to a file handle.
fputi - Writes an integer (4 bytes) to a file handle.
fputl - Writes a long (4 bytes) to a file handle. */
fputs(filepointer,writedata)
/*Second way*/
/*fprintf - Writes a line to a file */
fprintf(filepointer,"%s %s %d %f^n",steamid,name,anyvalue,anyfloatvalue)
/*close the file,this is optional,but better is it to close the file*/
fclose(filepointer)
}
}
public filereadtest(id){
/*open file in read-mode*/
new filepointer = fopen(filename,"r")
/*check if file is open,on an error filepointer is 0*/
if(filepointer)
{
new readdata[128],steamid[32],anyvalue,Float:anyfloatvalue
new parsedsteamid[32],parsedname[32],parsedanyvalue[8],parsedanyfloatvalue[8]
/*Read the file until it is at end of file*/
/*fgets - Reads a line from a text file -- includes newline!*/
while(fgets(filepointer,readdata,127))
{
parse(readdata,parsedsteamid,31,parsedname,31,parsedanyvalue,7,parsedanyfloatvalue,7)
get_user_authid(id,steamid,31)
if(equal(steamid,parsedsteamid))
{
anyvalue = str_to_num(parsedanyvalue)
anyfloatvalue = str_to_float(parsedanyfloatvalue)
client_print(id,print_chat,"Your saved Steamid:%s Name:%s Value:%d FloatValue:%f",parsedsteamid,parsedname,anyvalue,anyfloatvalue)
break
//...
}
}
fclose(filepointer)
}
}