Raised This Month: $ Target: $400
 0% 

Check if is in file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 12-25-2008 , 15:15   Check if is in file
Reply With Quote #1

Hi!
I want to check if a string is in a file.
For example: in the file i have:
Code:
BAILOPAN
Freecode Alka
Exolent
And i want to check if 'Alka' is in that file (space between strings, or new lines)

I tried using this, wich i found is no mine
PHP Code:
stock bool:is_string_in_file(file[], const string[])
{
   new 
iFile fopen(file"rt");

   if(!
iFile)
   return 
false;

   new 
szBuffer[64], szData[32];

   while(!
feof(iFile))
   {
      
fgets(iFileszBuffersizeof szBuffer 1);
      
trim(szBuffer);
 
      if(!
szBuffer[0] || szBuffer[0] == ';')
         continue;
 
      
parse(szBufferszDatasizeof szData 1);
 
      if(
equali(szDatastringstrlen(string)))
      {
         
fclose(iFile);
         return 
true;
      }
   }
   
fclose(iFile);
   return 
false;

but is not working, or using 'str_piece' from Alka's Extra precacher...ofcourse, i the file i've put instead of spaces a token.

Anyway, help would be apreciated.
__________________

anakin_cstrike is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-25-2008 , 15:42   Re: Check if is in file
Reply With Quote #2

PHP Code:
      if(equali(szDatastringstrlen(string))) 

PHP Code:
      if(containi(szDatastring) != -1
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 12-25-2008 , 16:02   Re: Check if is in file
Reply With Quote #3

Thanks, but is not working.
I tried like this:

PHP Code:
public client_connectid )
{
    static 
name32 ];
    
get_user_nameidnamesizeof name );
    
    if( 
IsInFile"name_list.txt"name ) )
        
server_cmd"kick #%d"get_user_useridid ) );
    
    return 
PLUGIN_CONTINUE;
}

stock bool:IsInFile(file[], const string[])
{
    new 
iFile fopenfile"rt" );

    if( !
iFile )
        return 
false;

    new 
szBuffer64 ], szData32 ];

    while( !
feofiFile ) )
    {
        
fgetsiFileszBuffersizeof szBuffer );
        
trimszBuffer );
 
        if( !
szBuffer] || szBuffer] == ';' )
            continue;
 
        
parseszBufferszDatasizeof szData );
 
        if( 
containiszDatastring ) != -1)
        {
            
fcloseiFile );
            return 
true;
        }
    }
    
    
fcloseiFile );
    
    return 
false;

__________________

anakin_cstrike is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 12-25-2008 , 16:28   Re: Check if is in file
Reply With Quote #4

PHP Code:
#include <amxmisc>

new g_szFile[64]
new Array:
g_hNameArray

public plugin_init()
{
    
get_configsdir(g_szFilecharsmax(g_szFile))
    
add(g_szFilecharsmax(g_szFile), "/names.cfg")
    
    
LoadConfig()
    
    
// Register a command that calls LoadConfig so you can reload the file whenever you want.
    // LoadConfig() returns the number of records read.
    
    // register_concmd(...)
}

public 
client_connect(id)
{
    static 
szName[32];
    
get_user_name(idszNamecharsmax(szName));
    
    if(
ArraySearchString(g_hNameArrayszName) != -1)
    
server_cmd("kick #%d"get_user_userid(id));
}

LoadConfig(iFatal false)
{
    if(!
g_hNameArray)
        
g_hNameArray ArrayCreate(32)
    else
        
ArrayClear(g_hNameArray)
        
    new 
hFile fopen(g_szFile"r")
    if(!
hFile)
    {
        
hFile fopen(g_szFile"w")
        if(!
hFile)
        {
            if(
iFatal)
            {
                new 
szFailState[64]
                
formatex(szFailStatecharsmax(szFailState), "Can't open/create file %s"g_szFile)
                
                
set_fail_state(szFailState)
            }
            
            return -
1
        
}
        else
        {
            
fclose(hFile)
            
            return 
0
        
}
    }
    
    static 
szBuffer[128], szSplitBuffer[128], szName[32]
    while(!
feof(hFile) && fgets(hFileszBuffercharsmax(szBuffer)))
    {
        
trim(szBuffer)
        
        if(!
szBuffer[0] || szBuffer[0] == ';' || equal(szBuffer"//"2))
            continue
        
        
copy(szSplitBuffercharsmax(szSplitBuffer), szBuffer)
        
        while(
szSplitBuffer[0])
        {
            
strbreak(szSplitBufferszNamecharsmax(szName), szSplitBuffercharsmax(szSplitBuffer))
            
            if(
szName[0])
                
ArrayPushString(g_hNameArrayszName)
        }
    }
    
fclose(hFile)
    
    return 
ArraySize(g_hNameArray)
}

ArraySearchString(const Array:hArray, const szSearch[], iCaseSensitive false)
{
    static 
iSize
    
if(!hArray || !(iSize ArraySize(hArray)))
        return -
1
    
    
static szTempString[32]
    for(new 
i=0iSizei++)
    {
        
ArrayGetString(hArrayiszTempStringcharsmax(szTempString))
        
        if(
iCaseSensitive equal(szSearchszTempString) : equali(szSearchszTempString))
            return 
i
    
}
    
    return -
1

__________________

Community / No support through PM
danielkza is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 12-25-2008 , 16:46   Re: Check if is in file
Reply With Quote #5

Thanks for that code...is working.
__________________

anakin_cstrike 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 09:08.


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