AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How do you read something from a file. Ini? (https://forums.alliedmods.net/showthread.php?t=125841)

CsIosefin 05-02-2010 17:46

How do you read something from a file. Ini?
 
Code:

#include <amxmodx>

public plugin_init()
{
    register_plugin("Auto-Messages", "1.0", "Team")
   
    register_dictionary("addons/amxmodx/configs/messages.ini")
    set_task(7.5, "Hud", _, _, _, "b")
    set_task(15.0, "Chat", _, _, _, "b")
}

public Chat(id, level, cid)
{
    client_print(0, print_chat, "%s", "Messages")
}
public Hud(id, level, cid)
{
    set_hudmessage(random(255), random(255), random(255), -1.0, 0.08, random_num(0, 2), 0.02, 6.0, 0.01, 0.1, 2)
    show_hudmessage(0, "%s", "Messages")
}

I want to do a self-messages, he read what was in messages.ini? How can I, as it did not even know it's never so interested ...
Messages.ini file is in addons / amxmodx / configs /

drekes 05-02-2010 18:04

Re: How do you read something from a file. Ini?
 
maybe this can help you out:

http://forums.alliedmods.net/showthread.php?t=46218

CsIosefin 05-03-2010 12:48

Re: How do you read something from a file. Ini?
 
Hmmm .... I want something simpler, I want to read from a file. Cars and display the plugin, so all ... is such a plugin then? From there I will try to source to see a way ...

drekes 05-03-2010 12:52

Re: How do you read something from a file. Ini?
 
i myself am no expert at reading files, writing to them is no problem though, some more experienced coders will help you out.
Just be patience

Sylwester 05-03-2010 13:40

Re: How do you read something from a file. Ini?
 
reading from file is easy:
PHP Code:

#include <amxmodx>
#include <amxmisc>


public func(){
    new 
path[128], fhcache[128]
    
get_configsdir(path127)
    
format(path127"%s%s"path"/myfile.ini")
    
//with this it will try to open file: amxmodx/configs/myfile.ini

    
if(!file_exists(path)){ //check if file exists
        
log_amx("Error: file does not exist (%s)."path)
        return
    }

    
fh fopen(path"rt"//open text ("t") file for reading ("r")
    
if(!fh){ //check if we got proper file handle
        
log_amx("Error: Could not open file (%s)."path)
        return
    }

    new 
example_counter
    
while(!feof(fh)){ //loop until file handle points at the end of file
        
fgets(fhcache127//read next line from file and store it in "cache"

        //example count lines and print them in server console using log_amx:
        
example_counter++
        
log_amx("loaded line number %d: %s"example_countercache)

    }

    
fclose(fh)  //close file



Bugsy 05-03-2010 13:55

Re: How do you read something from a file. Ini?
 
http://forums.alliedmods.net/showpos...7&postcount=13

CsIosefin 05-04-2010 10:16

Re: How do you read something from a file. Ini?
 
This method is unique? Why so much stuff that I want to remove something from a text file, that's all ... nothing else, but if so, better leave directly. Sma ...

drekes 05-04-2010 10:22

Re: How do you read something from a file. Ini?
 
there is so much stuff because many things has to be done to remove something.

Quote:

1. see if file exists
2. Read file
3. look for something
4. if something exists, remove it otherwise do nothing.
5. close the file

fysiks 05-04-2010 17:34

Re: How do you read something from a file. Ini?
 
Quote:

Originally Posted by CsIosefin (Post 1170195)
Why so much stuff that I want to remove something from a text file, that's all

Because that's the way it is. It's different for different uses. You will never find a "one command does it all for you".

CsIosefin 05-05-2010 12:54

Re: How do you read something from a file. Ini?
 
Should look like this? Or should include, depending? I think it's so pretty ... or at least, I do not give any erroare, I did not test him.
One question, if well, one must put the text in another? I do not want to read everything, but line by line ...
Code:

#include <amxmodx>
#include <amxmisc>

public plugin_init()
{
    register_plugin("Auto-Messages", "1.0", "Team")

    set_task(7.5, "Hud", _, _, _, "b")
    set_task(15.0, "Chat", _, _, _, "b")
}

public func()
{
        new path[128], fh, cache[128]
        get_configsdir(path, 127)
        format(path, 127, "%s%s", path, "/messages.ini")

        if(!file_exists(path))
        {
        log_amx("Error: file does not exist (%s).", path)
        return
        }

        fh = fopen(path, "rt")
        if(!fh)
        {
        log_amx("Error: Could not open file (%s).", path)
        return
        }

        new example_counter
        while(!feof(fh))
        {
        fgets(fh, cache, 127)
        example_counter++
        log_amx("loaded line number %d: %s", example_counter, cache)
        }

        fclose(fh)
}

public Chat(id)
{
        client_print(0, print_chat, "%s", "path")
}
public Hud(id)
{
        set_hudmessage(random(255), random(255), random(255), -1.0, 0.08, random_num(0, 2), 0.02, 6.0, 0.01, 0.1, 2)
        show_hudmessage(0, "%s", "path")
}



All times are GMT -4. The time now is 03:35.

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