AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reading Files (skipping lines, etc...) (https://forums.alliedmods.net/showthread.php?t=105174)

shadow.hk 10-02-2009 00:25

Reading Files (skipping lines, etc...)
 
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.

Emp` 10-02-2009 00:46

Re: Reading Files (skipping lines, etc...)
 
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;

shadow.hk 10-02-2009 00:53

Re: Reading Files (skipping lines, etc...)
 
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 :)

Emp` 10-02-2009 00:56

Re: Reading Files (skipping lines, etc...)
 
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.

fysiks 10-02-2009 20:21

Re: Reading Files (skipping lines, etc...)
 
@shadow: You can use trim(szLine) after fgets().

Quote:

Originally Posted by Emp` (Post 949771)
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.


All times are GMT -4. The time now is 22:32.

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