Raised This Month: $ Target: $400
 0% 

Reading Files (skipping lines, etc...)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
shadow.hk
Senior Member
Join Date: Dec 2008
Location: WA, Australia
Old 10-02-2009 , 00:25   Reading Files (skipping lines, etc...)
Reply With Quote #1

Hey, I have quite a few mods that use files to gather any required information, but I'm having trouble with getting it to skip blank lines properly.

I've tested it and it'll skip the ; fine, just not blank lines - so if you put this in the file.

Code:
; blah blah
thing1


thing2
thing3
thing4
The menu would appear like...

Code:
Menu:

1. thing1
2. thing1
3. thing1
4. thing2
5. thing3
6. thing4
Here's an example of the file...

PHP Code:
// Loads rank configuration settings
LoadRankFile()
{
    
// If file doesn't exist, make one
    
if( !file_exists(ranksfile) )
        
write_file(ranksfile"; Ranks Configuration file^n; Usage: <Rankname> score required>^n; Place in ascending order (i.e. Top rank is the lowest rank)");
    
    
// Set up some vars to hold parsing info
    
new szLine[32], szRankName[16], szScoreNeeded[4];
    
    
// Set max ranks to 0
    
g_MaxRanks 0;
    
    
// Open customization file for reading
    
new file fopen(ranksfile"rt");
    
    while( !
feof(file) )
    {
        
szLine[0] = '^0';
        
        
// Read one line at a time
        
fgets(fileszLine31);
        
        
// Blank line or comment
        
if( szLine[0] == ';' || !szLine[0] ) continue;
        
        else
        {
            
// Seperate the information
            
parse(szLineszRankName15szScoreNeeded3);
            
            
g_ScoreNeeded[g_MaxRanks] = str_to_num(szScoreNeeded);
            
g_szRankName[g_MaxRanks] = szRankName;
            
            
// If line equals hardcoded maximum ranks value, exit the loop
            
if( g_MaxRanks == MAX_BOARDS )
            {
                
log_amx("[SURF-RPG] Reached maximum ranks at ^"%s^"! Increase MAX_RANKS in the script to allow more"szRankName);
                break;
            }
            
            
// Increase ranks
            
g_MaxRanks++;
        }
    }
    
    
// Close the file
    
fclose(file);

Any help would be greatly appreciated.
shadow.hk is offline
Send a message via MSN to shadow.hk
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 10-02-2009 , 00:46   Re: Reading Files (skipping lines, etc...)
Reply With Quote #2

The file natives return a string containing the new line character '^n', so a blank line is really the string "^n".

So solution:
Code:
if( szLine[0] == ';' || !szLine[0] || szLine[0] == '^n' ) continue;
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
shadow.hk
Senior Member
Join Date: Dec 2008
Location: WA, Australia
Old 10-02-2009 , 00:53   Re: Reading Files (skipping lines, etc...)
Reply With Quote #3

ohhh... I see. So just replace szLine[0] with szLine[0] == '^n'...

PHP Code:
if( szLine[0] == ';' || !szLine[0] == '^n' ) continue; 
or do i need the whole...

PHP Code:
if( szLine[0] == ';' || !szLine[0] || szLine[0] == '^n' ) continue; 
and I'm presuming I can remove the 'else' part of the code as well? since continue excludes it from the rest of the loop?

Thanks for the quick response by the way
shadow.hk is offline
Send a message via MSN to shadow.hk
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 10-02-2009 , 00:56   Re: Reading Files (skipping lines, etc...)
Reply With Quote #4

Well !szLine[0] is essentially checking for an empty string. This would only occur if the last line in the file was a blank line. You could take it out if you want, but just to be safe I would leave it there.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-02-2009 , 20:21   Re: Reading Files (skipping lines, etc...)
Reply With Quote #5

@shadow: You can use trim(szLine) after fgets().

Quote:
Originally Posted by Emp` View Post
The file natives return a string containing the new line character '^n', so a blank line is really the string "^n".

So solution:
Code:
if( szLine[0] == ';' || !szLine[0] || szLine[0] == '^n' ) continue;
In windows the string would be "^r^n". So, you could add another check or just use trim as I suggest.
__________________
fysiks 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 22:32.


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