Raised This Month: $32 Target: $400
 8% 

Find the last line


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-13-2018 , 11:12   Find the last line
Reply With Quote #1

Hi,

How would I find the last line in a file? Let's say I've written these in my file:

Quote:
"This Is First Line"
"This Is Second Line"
"This Is Third Line"
So how would I find the fourth line with a stock and then use write_file with it, when I use -1 as the last parameter it mixes lines with each other.

If you still didn't understand, here's another explanation:

As I said before, this is what I've manually written in the file myself:

Quote:
"This Is First Line" --> So this supposedly is line number 0
"This Is Second Line" --> Line number 1
"This Is Third Line" --> Line number 2
--> This here isn't a line but it should be line number 3, how would I find it?
So basically I need to find the last line + 1.

This is what I've done so far but it isn't working properly.

PHP Code:
new g_iLine;

public 
OnAddNewAdminid )
{
    if( ~ 
get_user_flagsid ) & ADMIN_ADD_FLAG )
    {
        
client_printidprint_console"You have no Access to this command" );
        return 
PLUGIN_HANDLED;
    }

    if( 
read_argc( ) != )
    {
        
client_printidprint_console"Usage is : amx_addadmin <Nick|SteamID> <Password> <Flags> <Prefix>" );
        return 
PLUGIN_HANDLED;
    }

    new 
szAdminName32 ], szAdminPassword18 ], szAdminFlags32 ], szAdminPrefix18 ];
    
    
read_argv1szAdminNamecharsmaxszAdminName ) );
    
read_argv2szAdminPasswordcharsmaxszAdminPassword ) );
    
read_argv3szAdminFlagscharsmaxszAdminFlags ) );
    
read_argv4szAdminPrefixcharsmaxszAdminPrefix ) );
    
    if( ( 
strlenszAdminName ) < ) || ( strlenszAdminFlags ) < ) )
    {
        
client_printidprint_console"ERROR: Incorrect Format of Admin" );
        return 
PLUGIN_HANDLED;
    }

    
ReadFile( );
    
    new 
szConfigs64 ], szFormat128 ], szData128 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    
formatexszDatacharsmaxszData ), "^"%s^" ^"%s^" ^"%s^" ^"%s^""szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix );
    
write_fileszFormatszDataGetLinesszFormat ) );
    
    
client_printidprint_console"Successfully Added New Admin: Name: %s | Password: %s | Flags: %s | Prefix: %s |"szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix );
    return 
PLUGIN_CONTINUE;
}

ReadFile( )
{
    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix18 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    new 
iFile fopenszFormat"r" );
    
    if( 
iFile )
    {
        while( ! 
feofiFile ) )
        {    
            
fgetsiFileszPlayerDatacharsmaxszPlayerData ) );
            
trimszPlayerData );
            
            if( 
szPlayerData] == ';' || ( szPlayerData] == '/' && szPlayerData] == '/' ) )
            continue;
            
            while( 
szPlayerData] == EOS )
            
g_iLine--;
            
            
parseszPlayerDataszNamecharsmaxszName ), szPasswordcharsmaxszPassword ), szAccessFlagscharsmaxszAccessFlags ), szPrefixcharsmaxszPrefix ) );
            
            
g_iItemsPlayer_Name ] = szName;
            
g_iItemsPlayer_Password ] = szPassword;
            
g_iItemsPlayer_AccessFlags ] = szAccessFlags;
            
g_iItemsPlayer_Prefix ] = szPrefix;
            
            
ArrayPushArrayg_aDatabaseg_iItems );
            
            
g_iLine++;
        }
        
fcloseiFile );
    }
}

GetLines( const szFile[ ] )
{
    if( 
file_existsszFile ) )
    {
        new 
szData128 ], iTextLength;

        while( 
read_fileszFileg_iLineszDatacharsmaxszData ), iTextLength ) )
        {            
            if( 
szData] )
            {
                
g_iLine++;
                continue;
            }
        }
        return 
g_iLine;
    }
    return -
1;

I'm trying to re-create admin.sma as it is too old and poorly written. I've made it to be able to set admins by an .ini file (already done) and by an in-game command (doesn't work properly). So when you execute the command, it should automatically find a new line for the new entry.
__________________

Last edited by edon1337; 01-13-2018 at 11:19.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 01-13-2018 , 11:29   Re: Find the last line
Reply With Quote #2

Someone did not bother to read the documentation. There's flag "a" for fopen.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-13-2018 , 11:36   Re: Find the last line
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
Someone did not bother to read the documentation. There's flag "a" for fopen.
Sorry, I never thought fopen would have such option, what exactly does flag "a" do? It says 'append' in the Doc but I don't quite understand it..
__________________

Last edited by edon1337; 01-13-2018 at 11:37.
edon1337 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-13-2018 , 12:03   Re: Find the last line
Reply With Quote #4

More info on fopen()

http://pubs.opengroup.org/onlinepubs...ons/fopen.html

So you're just trying to add a line to the end of a file?

PHP Code:
new iFile fopen"file.txt" "a" );

fputsiFile "New line^r^n" );
    
fcloseiFile ); 
__________________

Last edited by Bugsy; 01-13-2018 at 12:10.
Bugsy is online now
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-13-2018 , 14:03   Re: Find the last line
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
More info on fopen()

http://pubs.opengroup.org/onlinepubs...ons/fopen.html

So you're just trying to add a line to the end of a file?

PHP Code:
new iFile fopen"file.txt" "a" );

fputsiFile "New line^r^n" );
    
fcloseiFile ); 
First of all, thanks for your reply, and the link. I'll take a look at it.
No, I just want to find the last line of the file and then increase it by 1 (that would take me to the line I'm searching for).

I already tried to explain my situation, let's say this is my .ini file with the infos:

Quote:
"edon"
"bugsy"
Now I have a command to set new entries to the .ini file via a console command.

So now we add a new entry:
PHP Code:
write_file"MyFile.ini""hamleteagle" ); 
By default the line of write_file is -1 (auto detect or something), it mixes lines up like this
Quote:
"edon"
"bugsy""hamleteagle"
My point is to ignore that mix, so it should be like this:
Quote:
"edon"
"bugsy"
"hamleteagle"
Hope you understood!
__________________
edon1337 is offline
E1_531G
Senior Member
Join Date: Dec 2017
Old 01-13-2018 , 14:29   Re: Find the last line
Reply With Quote #6

You should always write in the file using newline char at the end: fputs( iFile , "New line^n" );
If you write good first 2 lines, the 3th line will be writen fine.
__________________
My English is A0
E1_531G is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-13-2018 , 15:03   Re: Find the last line
Reply With Quote #7

That depends on the last person/app that wrote to the file.

You would either need to write "[newline]new line of text" or "new line of text[newline]"

To be safe, I would always read the 2 bytes at the end of the file prior to writing to check if the new line bytes exist, then you can conditionally include when when writing.
__________________
Bugsy is online now
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-13-2018 , 16:05   Re: Find the last line
Reply With Quote #8

This is what I was actually looking for, thanks anyways!
PHP Code:
    new szConfigs64 ], szFormat128 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    new 
iFile fopenszFormat"a" );
    
    
fprintfiFile"^n^"%s^" ^"%s^" ^"%s^" ^"%s^""szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix );
    
fcloseiFile ); 
__________________
edon1337 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-13-2018 , 16:14   Re: Find the last line
Reply With Quote #9

That's the same thing that I told, except that will always write starting with new-line.

So if the last written line included a new-line at the end, you will end up with a blank line in the file. IMO, it would be better to check the last 1/2 bytes for this before writing.

Code:
Last line in file

New line in file
__________________

Last edited by Bugsy; 01-13-2018 at 16:15.
Bugsy is online now
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-13-2018 , 17:02   Re: Find the last line
Reply With Quote #10

Quote:
Originally Posted by Bugsy View Post
it would be better to check the last 1/2 bytes for this before writing.
How would I check that?
__________________

Last edited by edon1337; 01-13-2018 at 17:03.
edon1337 is offline
Reply


Thread Tools
Display Modes

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 15:01.


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