AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to swap between lines? (https://forums.alliedmods.net/showthread.php?t=204373)

pokemonmaster 12-29-2012 16:33

How to swap between lines?
 
I have a file like this format
Code:

[STEAM_ID_1] // SteamId
Noob1 // name
13/9/2012 14:50:49 // last connect time

[STEAM_ID_2]
Noob2
12/9/2012 11:30:12

[STEAM_ID_3]
Noob3
21/12/2012 21:21:21

Let's assume that I'm in the first line, how to go to the second and the third line and read them using fgets() in the same function?

Here is my current code:
Code:
enum _:DATA {     NAME[32],     STEAMID[35],     LAST_CONNECT[50] } public admin_cmd(id, level, cid) {     if(!cmd_access(id, level, cid, 1))         return PLUGIN_HANDLED         CheckFile()         //console_print(id, "# %s %-22.22s %-22.22s", "Name", "SteamId", "Last Connect Time")     server_print("# %-22.22s %-22.22s %s", "Name", "SteamId", "Last Connect Time")         new f = fopen(FILE, "r")         if(!f)         return console_print(id, "Couldn't open file")         new szLine[50], Data[DATA]     while(!feof(f))     {         fgets(f, szLine, charsmax(szLine))                 if(!szLine[0] || szLine[0] == ';' || szLine[0] != '[')             continue;                 replace(szLine, charsmax(szLine), "[", "")         replace(szLine, charsmax(szLine), "]", "")         replace(szLine, charsmax(szLine), "^n", "")                 copy(Data[STEAMID], charsmax(Data[STEAMID]), szLine)         // Here i should get the name and last connection time                 server_print("%d %-22.22s %-22.22s %s", ++i, Data[NAME], Data[STEAMID], Data[LAST_CONNECT])     }         fclose(f)         return PLUGIN_HANDLED }

fysiks 12-29-2012 19:15

Re: How to swap between lines?
 
Just use fgets() again. However, you should consider putting all the information on a single line. Also, instead of putting a formatted date in there, you should just use a timestamp and then format that when you need to display it.

ConnorMcLeod 12-30-2012 07:12

Re: How to swap between lines?
 
Something like that i guess :

PHP Code:

    while(!feof(f))
    {
        
fgets(fszLinecharsmax(szLine))
        
trimszLine )
        
        if(!
szLine[0] || szLine[0] == ';' || szLine[0] != '[')
            continue;
        
        
replace(szLinecharsmax(szLine), "[""")
        
replace(szLinecharsmax(szLine), "]""")
        
// replace(szLine, charsmax(szLine), "^n", "") // used trim before
        
        
copy(Data[STEAMID], charsmax(Data[STEAMID]), szLine)

        
// Here i should get the name and last connection time
        
fgets(fData[NAME], charsmax(Data[NAME]))
        
trimData[NAME] )
        
        
fgets(fData[LAST_CONNECT], charsmax(Data[LAST_CONNECT]))
        
trimData[LAST_CONNECT] )
        
        
server_print("%d %-22.22s %-22.22s %s", ++iData[NAME], Data[STEAMID], Data[LAST_CONNECT])
    } 



All times are GMT -4. The time now is 13:26.

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