Raised This Month: $32 Target: $400
 8% 

Write to .txt file and put +1


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
jonatat
Senior Member
Join Date: Dec 2017
Old 04-19-2021 , 10:00   Write to .txt file and put +1
Reply With Quote #1

Hello folks. I try to make simple thing. I need to wirte to .txt file and put +1. Example: I got file called numbers.txt, in this file i got number 1. I need to add +1 to that file and get 2.

My script:

PHP Code:
public WriteID()
{
new 
fp fopen("addons/amxmodx/configs/numbers.txt""wt");
new 
text[32] = fp++;
fputs(fptext);
fclose(fp);

But getting error then compiling. Any ideas what im doing wrong? Thanks u for helping me guys!
jonatat is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-19-2021 , 10:09   Re: Write to .txt file and put +1
Reply With Quote #2

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Plugin", "Version", "Author")     new filename[128]     formatex(filename[get_configsdir(filename, charsmax(filename))], charsmax(filename), "/numbers.txt")     new file = fopen(filename, "r+t")     new buffer[15]     new num = 0     if (fgets(file, buffer, charsmax(buffer)))         num = str_to_num(buffer) + 1     num_to_str(num, buffer, charsmax(buffer))     fputs(file, buffer)     fclose(file) }
__________________








CrazY. is offline
jonatat
Senior Member
Join Date: Dec 2017
Old 04-19-2021 , 10:13   Re: Write to .txt file and put +1
Reply With Quote #3

Quote:
Originally Posted by CrazY. View Post
Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Plugin", "Version", "Author")     new filename[128]     formatex(filename[get_configsdir(filename, charsmax(filename))], charsmax(filename), "/numbers.txt")     new file = fopen(filename, "r+t")     new buffer[15]     new num = 0     if (fgets(file, buffer, charsmax(buffer)))         num = str_to_num(buffer) + 1     num_to_str(num, buffer, charsmax(buffer))     fputs(file, buffer)     fclose(file) }
Thanks, working. BUT in file i'm getting now "1213" And can u make to BE: A1, A2, A3 and etc? Thanks CrazY

Last edited by jonatat; 04-19-2021 at 10:13.
jonatat is offline
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-19-2021 , 10:22   Re: Write to .txt file and put +1
Reply With Quote #4

Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Plugin", "Version", "Author")     new filename[128]     formatex(filename[get_configsdir(filename, charsmax(filename))], charsmax(filename), "/numbers.txt")     new file = fopen(filename, "r+t")     if (file)     {         new buffer[15]         new num = 0         if (fgets(file, buffer, charsmax(buffer)))             num = str_to_num(buffer[1]) + 1         fseek(file, 0, SEEK_SET)         fprintf(file, "A%d", num)         fclose(file)     } }
__________________









Last edited by CrazY.; 04-19-2021 at 10:24.
CrazY. is offline
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 04-19-2021 , 10:22   Re: Write to .txt file and put +1
Reply With Quote #5

Code:
#include <amxmodx>
#include <amxmisc>

public plugin_init()
{
	new g_szFileName[128];
	formatex(g_szFileName[get_configsdir(g_szFileName, charsmax(g_szFileName))], charsmax(g_szFileName), "/file.txt");
	new p_file = fopen(g_szFileName, "r+t");
	new szBuffer[16], num_to_string[16];
	new p_index_num = 0;
	new p_max_number = 1500;

	if(fgets(p_file, szBuffer, charsmax(szBuffer)))
	{
		while(str_to_num(g_szBuffer) <= p_max_number)
			p_index_num++;

		num_to_str(p_index_num, num_to_string, charsmax(num_to_string));
		fpus(p_file, num_to_string);
	}

	fclose(p_file);
}
not tested.
__________________
LondoN is offline
jonatat
Senior Member
Join Date: Dec 2017
Old 04-19-2021 , 10:24   Re: Write to .txt file and put +1
Reply With Quote #6

Quote:
Originally Posted by CrazY. View Post
Code:
#include <amxmodx> #include <amxmisc> public plugin_init() {     register_plugin("Plugin", "Version", "Author")     new filename[128]     formatex(filename[get_configsdir(filename, charsmax(filename))], charsmax(filename), "/numbers.txt")     new file = fopen(filename, "r+t")     if (file)     {         new buffer[15]         new num = 0         if (fgets(file, buffer, charsmax(buffer)))             num = str_to_num(buffer[1]) + 1         fseek(file, 0, SEEK_SET)         formatex(buffer, charsmax(buffer), "A%d", num)         fputs(file, buffer)         fclose(file)     } }
WORKING! Thanks CrazY
jonatat is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-19-2021 , 10:45   Re: Write to .txt file and put +1
Reply With Quote #7

Quote:
Originally Posted by LondoN View Post
Code:
#include <amxmodx>
#include <amxmisc>

public plugin_init()
{
	new g_szFileName[128];
	formatex(g_szFileName[get_configsdir(g_szFileName, charsmax(g_szFileName))], charsmax(g_szFileName), "/file.txt");
	new p_file = fopen(g_szFileName, "r+t");
	new szBuffer[16], num_to_string[16];
	new p_index_num = 0;
	new p_max_number = 1500;

	if(fgets(p_file, szBuffer, charsmax(szBuffer)))
	{
		while(str_to_num(g_szBuffer) <= p_max_number)
			p_index_num++;

		num_to_str(p_index_num, num_to_string, charsmax(num_to_string));
		fpus(p_file, num_to_string);
	}

	fclose(p_file);
}
not tested.
Smells like infinite loop.
__________________
HamletEagle is offline
Reply


Thread Tools
Display Modes

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 17:28.


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