AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Read/writeing in files (https://forums.alliedmods.net/showthread.php?t=45941)

stigma 10-14-2006 22:15

Read/writeing in files
 
If i have a file called gNames.txt and to search the entire file, for an steamID, what would i do then?

And if i have a line saying: "stigma B 21", then what should i do if i want my plugin to read the second word, which is equal to "B"?

stupok 10-15-2006 00:10

Re: Read/writeing in files
 
Search the entire file for a steamid:
Code Snippet:
Code:
new pAuth[33], Len, Text[101], File[32]         get_user_authid(id, pAuth, 32)         if(is_user_bot(id))                 return PLUGIN_CONTINUE         new lines = file_size(szFile, 1)         for(new i=0; i<lines; i++) {                 read_file(File, i, Text, 100, Len)                 if(equali(Text, pAuth)) {                         //code...                 }         }

To read the second word:
Code snippet:
Code:
new File[33], Text[101], something[5], Bthing[33], somethingelse[33], Len, i read_file(File, i, Text, 100, Len) parse(Text, something, 4, Bthing, 32, somethingelse, 32) //the value B from "stigma B 21" would be held in the array called Bthing

Or, just look at my plugin called Users List.

schnitzelmaker 10-15-2006 08:13

Re: Read/writeing in files
 
Another way are the new filehandle commands
Code:
new authid[32],steamid[32],array[32] get_user_authid(id,authid,31) new fp=fopen("gNames.txt","rw")//open file in read-write mode while(!feof(fp)) //read lines until end-of-file {     fgets (fp,array,31)//read a line from file     parse(array,steamid,31)     if(equali(steamid,authid))//check if steamid(in file) is same as authid from player     {         client_print(0,print_chat,"steamid==authid:%s",authid,steamid)         //your code here...     } }

[ --<-@ ] Black Rose 10-15-2006 08:52

Re: Read/writeing in files
 
search for authid:
Code:
search_file_for_auth(file[] = "addons/amxmodx/...", match_auth[]) {     new line, text[64], len         while ( read_file(file, line++, text, 63, len) ) {                 if ( contain(text, match_auth) != -1 )             return 1 // match     }     return 0 // no match }

Code:
new line, text[64], second_word[2], len, file[] = "addons/amxmodx/..." while ( read_file(file, line++, text, 63, len) ) {         strbreak(text, text, 0, second_word, 1) }

parse() is bad

Zenith77 10-15-2006 10:52

Re: Read/writeing in files
 
You should know better than to hardcode paths :nono:

get_localinfo();
get_configsdir();

schnitzelmaker 10-15-2006 11:29

Re: Read/writeing in files
 
Quote:

parse() is bad
Not true:"strbreak" only support 2 words,where you know the lenght of the words,it dont support spaces,tabs,newlines,...

"strbreak" is not constructed for filehandle.It is for like "replace",search a string inside and break them into 2 strings.

Zenith77 10-15-2006 11:32

Re: Read/writeing in files
 
Just because I native has a good purpose/use doesn't mean the implementation of it is. I'm not saying it is bad, just bringing up a point.


All times are GMT -4. The time now is 04:50.

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