AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Write to .txt file and put +1 (https://forums.alliedmods.net/showthread.php?t=332026)

jonatat 04-19-2021 10:00

Write to .txt file and put +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!

CrazY. 04-19-2021 10:09

Re: Write to .txt file and put +1
 
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) }

jonatat 04-19-2021 10:13

Re: Write to .txt file and put +1
 
Quote:

Originally Posted by CrazY. (Post 2744516)
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

CrazY. 04-19-2021 10:22

Re: Write to .txt file and put +1
 
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)     } }

LondoN 04-19-2021 10:22

Re: Write to .txt file and put +1
 
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.

jonatat 04-19-2021 10:24

Re: Write to .txt file and put +1
 
Quote:

Originally Posted by CrazY. (Post 2744518)
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 :grrr:

HamletEagle 04-19-2021 10:45

Re: Write to .txt file and put +1
 
Quote:

Originally Posted by LondoN (Post 2744519)
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.


All times are GMT -4. The time now is 02:31.

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