AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [HELP] Creatting and editting file (https://forums.alliedmods.net/showthread.php?t=108995)

Mxnn 11-11-2009 22:08

[HELP] Creatting and editting file
 
Hi! I'm here again u.u.
In this case i have to create and writting or delete a external file..
I do this :
PHP Code:

new fileban[256]
stock bool:cont_exists(file[],name[])
{
    new 
file_pointer fopen(file"rt")
    
    new 
Buffer[256]
    
    while (!
feof(file_pointer))
    {
        
fgets(file_pointerBuffer31)
        
        
        if(
containi(Buffername) != -1)
        {
            
fclose(file_pointer)
            return 
true
        
}
    }
    return 
false
}
.....
public 
plugin_init() {
.....
    
check_file()
    
}
.....
        new 
file fopen(fileban"wt")
        
fprintf(file"^n%s"szTargetName//Here i add this line to the file
        
fclose(file)
.....
if(
file)
    {
        new 
Buffer[128]
        new 
counter
        
        
while(!feof(file))
        {
            
fgets(fileBuffer128)
        
            
parse(BufferBuffersizeof Buffer -1)
            
            if(
equal(Buffername))
            {    

            
write_file(fileban,"",counter// Here i replace the line "counter" by ""

            
            
}
            
counter++
        }

    
        
fclose(file)
    }
return 
PLUGIN_HANDLED
}
public 
check_file()
{
    new 
datadir[64]
    
get_datadir(datadir63)
    
    
format(filebansizeof fileban 1"%s/asd.ini"datadir)
    
    new 
file_pointer fopen(fileban"at+")
    
    if(!
file_pointer)
    {
        
write_file(fileban,"//File created",0)
    }
    
    if(
file_pointer)
    {
        
server_print("The file %s was created"fileban)
        
write_file(fileban""0)
    } 
    
fclose(file_pointer)


This is the part of the code that have connection with the external file.
The file is never created and can't edit anything or read it because it doesn't exist.

Any help?

My greets and thanks! & sorry for my english

fysiks 11-12-2009 00:05

Re: [HELP] Creatting and editting file
 
write_file() does not go with fopen().

You need to use commands that work with fopen() like fputs() or fprintf() and the like. There is probably more that I don't know about but this is what I did see that is wrong.

Mxnn 11-12-2009 15:07

Re: [HELP] Creatting and editting file
 
But if i want to replace a line from the file?
How i would do it?

Exolent[jNr] 11-12-2009 15:12

Re: [HELP] Creatting and editting file
 
http://forums.alliedmods.net/showpos...04&postcount=3

Mxnn 11-12-2009 15:32

Re: [HELP] Creatting and editting file
 
But that code it's too large! I don't understand it :S

fysiks 11-12-2009 18:51

Re: [HELP] Creatting and editting file
 
Just put that stuff at the end of your file and all you need to do is use the functions. You don't really need to know anything. So the fact that it is "too long" means nothing. Just use the functions: ReplaceLineByNumber() and ReplaceLineByString().

:)

Mxnn 11-12-2009 22:27

Re: [HELP] Creatting and editting file
 
Sorry for my ignorance but if i want to add a text in the file how would i do?
If i want to replace a line by " " i would to use ReplaceLineByString, right?
I don't know who serves of ReplaceLineByNumber.. :(

Sorry again my ignorance. I want to learn the method

Exolent[jNr] 11-12-2009 23:18

Re: [HELP] Creatting and editting file
 
You can use this function:
Code:
FileContainsString( const szFilename[ ], const szString[ ], bool:bCaseSensitive=false ) {     new iFile = fopen( szFilename, "rt" );     if( !iFile ) {         return -1;     }         new szData[ 1024 ], iLine;         while( !feof( iFile ) ) {         fgets( iFile, szData, 1023 );                 if( bCaseSensitive ? ( contain( szData, szString ) != -1 ) : ( containi( szData, szString ) != -1 ) ) {             fclose( iFile );                         return iLine;         }                 iLine++;     }         fclose( iFile );         return -1; }

Then do code like this:
Code:
new iLine = FileContainsString( "myfile.txt", "some string" ); if( iLine >= 0 ) {     ReplaceLineByNumber( "myfile.txt", iLine, "replaced string" ); }

Mxnn 11-12-2009 23:35

Re: [HELP] Creatting and editting file
 
This is for replace a line, and if i want to write a line? Sorry again :( I know how to code but this is new for me. :(


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

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