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-18-2014 , 02:27   Re: Writing into a file
Reply With Quote #1

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 #2

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
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 21:10.


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