Raised This Month: $ Target: $400
 0% 

Writing into a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bladell
Senior Member
Join Date: Jun 2012
Old 06-17-2014 , 12:22   Writing into a file
Reply With Quote #1

Hi everyone, I am trying to make a plugin wich show a menu with a question and some answers read from a file, it's like a pool for players... all information should be saved to a file. Here is my problem, fprintf isn't working for me. The menu is appearing fine, but selected option isn't saved how it should be.

Any advice is welcome....

Last edited by Bladell; 06-17-2014 at 16:09.
Bladell is offline
4dead
Member
Join Date: Jun 2012
Old 06-17-2014 , 14:24   Re: Writing into a file
Reply With Quote #2

Try with this One
4dead is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 06-17-2014 , 14:36   Re: Writing into a file
Reply With Quote #3

fputs()
jimaway is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-17-2014 , 16:02   Re: Writing into a file
Reply With Quote #4

Quote:
Originally Posted by 4dead View Post
Try with this One
Do NOT use write_file().

Quote:
Originally Posted by jimaway View Post
fputs()
fputs() can't format but I guess in this case, he already formatted the message so this should work.
__________________
fysiks is offline
Bladell
Senior Member
Join Date: Jun 2012
Old 06-18-2014 , 02:27   Re: Writing into a file
Reply With Quote #5

None of them are working...

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

#define NAME    "Weekly question"
#define VERSION    "1.0"
#define AUTHOR    "Marius"

new file1[64], file2[64], file3[64], file4[64];
new 
question[128], answer[16][64], 0point[16], total 0percent[16];

public 
plugin_init()
{
    
register_plugin(NAMEVERSIONAUTHOR);
    
    
exec_files();
}

public 
exec_files()
{
    
get_configsdir(file163);
    
get_configsdir(file263);
    
get_configsdir(file363);
    
get_configsdir(file463);    
    
    
add(file1sizeof(file1) - 1"/qofw/questions.ini");
    
add(file2sizeof(file2) - 1"/qofw/answers.txt");
    
add(file3sizeof(file3) - 1"/qofw/stats.log");
    
add(file4sizeof(file4) - 1"/qofw/names.log");
    
    new 
fHandle fopen(file1"rt");    
    
    if(!
file_exists(file1))
    {
        
log_to_file("Questions.log""%s nu exista"file1);
        return 
PLUGIN_HANDLED
    
}
    
    if(!
file_exists(file2))
    {
        
log_to_file("Questions.log""%s nu exista"file2);
        return 
PLUGIN_HANDLED
    
}
    
    if(!
file_exists(file3))
    {
        
log_to_file("Questions.log""%s nu exista"file3);
        return 
PLUGIN_HANDLED
    
}
    
    if(!
file_exists(file4))
    {
        
log_to_file("Questions.log""%s nu exista"file4);
        return 
PLUGIN_HANDLED
    
}
    
    
fgets(fHandlequestion127);
    
    while(!
feof(fHandle))
    {
        
c++;
        
fgets(fHandleanswer[c], 63);
        
//Work away comments
        
if(answer[c][0] == ';' || !answer[c][0] || answer[c][0] == ' ' || answer[c][0] == 10
        {
            
c--;
            continue;
        }
    }
    
    
fclose(fHandle)
    
    return 
1
}

public 
rs_logged_in(id)
{    
    new 
name[64], name2[64], 0;
    
get_user_name(idnamecharsmax(name));
    new 
fHandle fopen(file4"rt")
    
    while(
fgets(fHandlename2charsmax(name2))) 
    {
        if(
equali(name2name)) 
            
j++;
    }
    
    if(!
j)
        
set_task(10.0"menu_app"id 31)
        
    
fclose(fHandle)
}

public 
menu_app(id)
{
    
id id 31;
    
    new 
menutext1[256], text2[256], len;
    
    
formatex(text1charsmax(text1), "\rQuestion of week:\w %s"question);
    
text1[strlen(text1) - 1] = 0;
    
text1[strlen(text1) - 2] = 0;
    
    
menu menu_create(text1"menu_handler");
    
    for(new 
1<= ci++)
    {
        
len formatex(text2charsmax(text2), "\w%s"answer[i]);
        
text2[len 1] = 0;
        
text2[len 2] = 0;
        
menu_additem(menutext2""0);
    }
    
    
menu_additem(menu"I'm indifferent, close this menu"""0);
    
menu_display(idmenu0);
    
    return
}

public 
read_info()
{
    new 
fHandle fopen(file3"rt");
    new 
0readData[16];
    
    while(!
feof(fHandle))
    {
        
i++;
        
fgets(fHandlereadData15);
        
point[i] = str_to_num(readData);
        
        
total total point[i];
    }
    
    
fclose(fHandle)
}

public 
write_info()
{
    new 
fHandle fopen(file2"w");
    new 
CurrentTime[64], text1[256], text2[256];
    
    
get_time("%d.%m.%Y - %H:%M:%S",CurrentTime,63
    
    
formatex(text1charsmax(text1), "###################################### %s ######################################"CurrentTime)
    
    
//write_file(fHandle, text1, line);
    //fprintf(fHandle, "%s^n", text1);
    
fputs(fHandletext1)
    
    for(new 
1<= ci++)
    {
        if(
point[i] != 0)
            
percent[i] = point[i] * 100 total;
    }
    
    new 
len 0;
    
    for(new 
1<= ci++)
    {
        
len formatex(text2charsmax(text2), answer[i])
        
len len formatex(text2[len], charsmax(text2) - len" - %i%%%%^n"
        
//write_file(file2, text2, 1);
        //fprintf(fHandle, "%s^n", text2);
        
fputs(fHandletext2)
    }
    
    
fclose(fHandle)
}

public 
menu_handler(idmenuitem
{
    new 
name[64]
    
    
read_info();
    
    
get_user_name(idnamecharsmax(name))
    
    switch(
item)
    {
        case 
0:
        {
            if(
<= c)
                
point[1]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
1:
        {
            if(
<= c)
                
point[2]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
2:
        {
            if(
<= c)
                
point[3]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
3:
        {
            if(
<= c)
                
point[4]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
4:
        {
            if(
<= c)
                
point[5]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
5:
        {
            if(
<= c)
                
point[6]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
6:
        {
            if(
<= c)
                
point[7]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
7:
        {
            if(
<= c)
                
point[8]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
        case 
8:
        {
            if(
<= c)
                
point[9]++;
            else
            {
                
menu_destroymenu );
                return 
PLUGIN_HANDLED;
            }
        }
    }
    
    new 
fHandle fopen(file4"w")
    
add(namecharsmax(name), "^n")
    
//fprintf(fHandle, "%s^n", name)
    
fputs(fHandlename)
    
    
menu_destroy(menu);
    
write_info();
    
info_refresh();
    
    
fclose(fHandle)
    
    return 
PLUGIN_HANDLED;
}

public 
info_refresh()
{
    new 
fHandle fopen(file3"w"), text[64];
    new 
line 01;
    
    for(
1<= ci++)
    {
        
formatex(textcharsmax(text), "%i^n")
        
fputs(fHandletext)
    }
    
    
fclose(fHandle)

I checked the permissions of file, is 777, I don't know why isn't working. It's interesting that if I check what's the date when the file was last time modified, it show me that it was modified after plugin execution. Also, formatex is used right, because I used log_to_file to check it... still don't know what is the problem.
Later edit: I tested this plugin locally to, same result..

Last edited by Bladell; 06-18-2014 at 02:40.
Bladell is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 02:59   Re: Writing into a file
Reply With Quote #6

Both of the formatex() functions where you use %i, you do it incorrectly. You must specify the variable that you want inserted into that place holder. I'm surprised you are not getting run-time errors for this.

Where exactly do you have this issue? There are several place where you try to write to a file. If it's all of them, I recommend that you create a small test plugin that simply writes to a file with the method that you are trying to do. I would start with not doing any formatting and simply write some hard coded text first. Then, if that works, add in some formatting and see if it works. If not, something is going wrong with the formatting.
__________________
fysiks is offline
Bladell
Senior Member
Join Date: Jun 2012
Old 06-18-2014 , 03:22   Re: Writing into a file
Reply With Quote #7

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

#define NAME    "Weekly question"
#define VERSION    "1.0"
#define AUTHOR    "Marius"

new file1[64];

public 
plugin_init()
{
    
register_plugin(NAMEVERSIONAUTHOR);
    
    
exec_files();
}

public 
exec_files()
{
    
get_configsdir(file163);
    
    
add(file1sizeof(file1) - 1"/qofw/questions.ini");
    
    if(!
file_exists(file1))
    {
        
log_to_file("Questions.log""%s nu exista"file1);
        return 
PLUGIN_HANDLED
    
}
    
    
    return 
1
}


public 
rs_logged_in(id)
{    
    new 
name[64], text[64], 0;
    
get_user_name(idnamecharsmax(name));
    new 
fHandle fopen(file1"wt")
    
    
formatex(textcharsmax(text), "%s fputs"name)
    
fputs(fHandletext)
    
write_file (file1"write_file"3)
    
fprintf(fHandle"%s frpint"name);
    
    
fclose(fHandle)

All of this three methods are working with this test plugin... wich of them should I use and what should I modify to my plugin?

" I'm surprised you are not getting run-time errors for this."
I'm getting run-time errors

Code:
L 06/18/2014 - 09:22:00: [AMXX] Displaying debug trace (plugin "qofweek.amxx")
L 06/18/2014 - 09:22:00: [AMXX] Run time error 11: divide 
L 06/18/2014 - 09:22:00: [AMXX]    [0] qofweek.sma::write_info (line 141)
L 06/18/2014 - 09:22:00: [AMXX]    [1] qofweek.sma::menu_handler (line 266)
I suppose that this errors appear because this plugin have nothing to read from file.
Bladell is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-18-2014 , 06:40   Re: Writing into a file
Reply With Quote #8

Never use write_file() or read_file(), period, especially when already using the fopen() method. Always use the fopen(), fclose(), etc. method.

Also, that error has nothing to do with what I was referring to.
__________________

Last edited by fysiks; 06-18-2014 at 06:41.
fysiks is offline
Bladell
Senior Member
Join Date: Jun 2012
Old 06-18-2014 , 16:50   Re: Writing into a file
Reply With Quote #9

I rewritten the entire plugin because had lots of bugs, it is working fine now, I hope that tomorrow I will finish it.
Thanks for advice and help fysiks, writing a test plugin was a great ideea for me
Bladell is offline
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 06-18-2014 , 20:42   Re: Writing into a file
Reply With Quote #10

Quote:
Originally Posted by fysiks View Post
Do NOT use write_file().
Any reason why it should be avoided?
OvidiuS is offline
Send a message via Skype™ to OvidiuS
Reply



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 12:58.


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