Raised This Month: $ Target: $400
 0% 

read_file again...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Br41n
Junior Member
Join Date: Apr 2006
Location: Magdeburg, Germany
Old 12-14-2006 , 15:51   read_file again...
Reply With Quote #1

Hi,
i'm working on a plugin like autobind, but the difference is, that the player can decide, if the player wants to bind the keys automatically or not..
For it i want to read a config.ini file with the key and the command for the key in it.. The file should look like this:

Code:
; Syntax: "<key>" "<command>"
"o" "say hello"
I tried to realize that part, but it doesn't work...
My Code:

PHP Code:
public autobind(idkeyparsedkeysparsedcommand)
{
    
//key will start at zero
    
if (key == 0//if player pressed Yes
    
{
        new 
readdata[128],parsedkeys[32],parsedcommand[32],txtlen
    
new line 0
    
while(!read_file(filename,line++,readdata,127,txtlen))
    {
    
parse(readdata,parsedkeys,32,parsedcommand,32)
    }   
        
client_cmd(id"say test")
    
client_print(idprint_chat"KEY: ^"%s^" COMMAND: ^"%s^" "parsedkeysparsedcommand)


    } else if (
key == 1) { // if player pressed No
         
client_print(idprint_chat"OK, no binding was changed. Type /help to see a list of available say commands.")
    }

I just tried a client_print to check, that read_file part works.. I want, when a player pressed 1 (Yes), the client_print tells this: "KEY: o COMMAND: say hello".. It's he first time, that I use read_file, so could anyone explain how to realize this?

Thanks
Br41n
__________________

Last edited by Br41n; 12-14-2006 at 15:53.
Br41n is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 12-14-2006 , 15:55   Re: read_file again...
Reply With Quote #2

Use the new file natives.
EDIT: Something like this..?
Code:
public autobind(id, key) {         switch ( key ) { // switches are faster than if, else if.         case 0: {             new readdata[80], parsedkeys[32], parsedcommand[32]                         new fileh = fopen(filename, "r") // open file.                         if ( ! fileh ) // if no file, return.                 return                         while ( ! feof(fileh) ) { // while not in end of file, continue to read.                 fgets(fileh, readdata, 79) // gets line text.                 strbreak(readdata, parsedkeys, 31, parsedcommand, 31) // strbreak is better than parse, also, 31, not 32.                 client_print(id, print_chat, "KEY: ^"%s^" COMMAND: ^"%s^" ", parsedkeys, parsedcommand) // maby you wanted to print this on EVERY command(?)             }             fclose(fileh) // never forget to close the file         }                 case 1: client_print(id, print_chat, "OK, no binding was changed. Type /help to see a list of available say commands.")     } }

Last edited by [ --<-@ ] Black Rose; 12-14-2006 at 16:11.
[ --<-@ ] Black Rose is offline
Br41n
Junior Member
Join Date: Apr 2006
Location: Magdeburg, Germany
Old 12-14-2006 , 17:04   Re: read_file again...
Reply With Quote #3

Yeah.. thank you, i will try it and will argue with it..

EDIT: One Question: What does the 31 in strbreak() mean?
__________________

Last edited by Br41n; 12-14-2006 at 17:41.
Br41n is offline
Old 12-14-2006, 19:51
schnitzelmaker
This message has been deleted by schnitzelmaker.
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 12-14-2006 , 20:22   Re: read_file again...
Reply With Quote #4

Quote:
Originally Posted by schnitzelmaker View Post
strbreak is the same as parse,but only for 2 strings and you must know the lenght of the strings.
Incorrect.
Quote:
Originally Posted by schnitzelmaker View Post
Spaces are NOT break it
Wrong again.
Quote:
Originally Posted by schnitzelmaker View Post
strbreak always break at the position who is given.
Guess what, wrong.
Quote:
Originally Posted by schnitzelmaker View Post
"feof" have a little problem inside,while using it you read the last line double times
Nope.
Quote:
Originally Posted by schnitzelmaker View Post
better use
Code:
while (fgets(fileh, readdata, 79) ) {
Nawh.
Quote:
Originally Posted by schnitzelmaker View Post
fgets return 0 if it cant read the line.
No, it returns strlen. If there was a line with no text your code would stop reading the file.

Wanna try again?

Summary: STFU! You have no idea what you're talking bout.


Quote:
Originally Posted by Br41n View Post
EDIT: One Question: What does the 31 in strbreak() mean?
Its the maxlen of the output string.

Last edited by [ --<-@ ] Black Rose; 12-14-2006 at 20:58.
[ --<-@ ] Black Rose is offline
Br41n
Junior Member
Join Date: Apr 2006
Location: Magdeburg, Germany
Old 12-17-2006 , 09:28   Re: read_file again...
Reply With Quote #5

OK.. thank you.. now, i got the problem with reading a file working.. but there is one more smaall problem... The script reads the commented lines also... All lines with comments I started with ;..

Code:
; Here is an Example:
; Syntax: <key> <command>
; Now, go on!
; ~~~~~~~~~~~~~~~~~
"m" "amx_menu"
So what to do? The script should not read the ; started lines, only the lines without ;...

Thanks
Br41n
__________________
Br41n is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 12-17-2006 , 11:18   Re: read_file again...
Reply With Quote #6

That's easy
Code:
public autobind(id, key) {         switch ( key ) {         case 0: {             new readdata[80], parsedkeys[32], parsedcommand[32]                         new fileh = fopen(filename, "r")                         if ( ! fileh )                 return                         while ( ! feof(fileh) ) {                 fgets(fileh, readdata, 79)                 if ( readdata[0] == ';' ) // add this line                     continue // add this line                 strbreak(readdata, parsedkeys, 31, parsedcommand, 31)                 client_print(id, print_chat, "KEY: ^"%s^" COMMAND: ^"%s^" ", parsedkeys, parsedcommand)             }             fclose(fileh)         }                 case 1: client_print(id, print_chat, "OK, no binding was changed. Type /help to see a list of available say commands.")     } }
[ --<-@ ] Black Rose is offline
Br41n
Junior Member
Join Date: Apr 2006
Location: Magdeburg, Germany
Old 12-17-2006 , 12:10   Re: read_file again...
Reply With Quote #7

ok.. thanks.. i tried the same if request, but a little bit wrong... ^^

PHP Code:
if( readdata[0] != ';') {  // ignore comments while reading file
                      
strbreak(readdataparsedkeys31parsedcommand31)
                      
client_print(idprint_chat"[SkBinds] KEY: ^"%s^" COMMAND: ^"%s^" | This is only a test, we are working on a new plugin..."parsedkeysparsedcommand// maby you wanted to print this on EVERY command(?)
           

__________________
Br41n is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 12-17-2006 , 17:52   Re: read_file again...
Reply With Quote #8

that should also work... if you're file doesn't look like this
Since the ';' must be at the first "place" in the row, no spaces in front of it...
Code:
;code
    ;code
  ;code
[ --<-@ ] Black Rose is offline
dutchmeat
Senior Member
Join Date: Sep 2006
Old 12-18-2006 , 04:16   Re: read_file again...
Reply With Quote #9

You could search all tokens in the line, if the isn't blank(space, hard space) check if it's a ';' then return 1.
But ofcourse you can just get the file to normal.
__________________
before you criticize someone, you should walk a mile in their shoes. that way, when you criticize them, you're a mile away and you have their shoes.
dutchmeat 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 06:48.


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