Raised This Month: $ Target: $400
 0% 

[HELP] Creatting and editting file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 11-11-2009 , 22:08   [HELP] Creatting and editting file
Reply With Quote #1

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

Last edited by Mxnn; 11-11-2009 at 22:11.
Mxnn is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-12-2009 , 00:05   Re: [HELP] Creatting and editting file
Reply With Quote #2

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.
__________________
fysiks is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 11-12-2009 , 15:07   Re: [HELP] Creatting and editting file
Reply With Quote #3

But if i want to replace a line from the file?
How i would do it?
Mxnn is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-12-2009 , 15:12   Re: [HELP] Creatting and editting file
Reply With Quote #4

http://forums.alliedmods.net/showpos...04&postcount=3
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 11-12-2009 , 15:32   Re: [HELP] Creatting and editting file
Reply With Quote #5

But that code it's too large! I don't understand it :S

Last edited by Mxnn; 11-12-2009 at 15:46.
Mxnn is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-12-2009 , 18:51   Re: [HELP] Creatting and editting file
Reply With Quote #6

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().

__________________
fysiks is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 11-12-2009 , 22:27   Re: [HELP] Creatting and editting file
Reply With Quote #7

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
Mxnn is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-12-2009 , 23:18   Re: [HELP] Creatting and editting file
Reply With Quote #8

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" ); }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Mxnn
Veteran Member
Join Date: Aug 2009
Location: AT MY HOME
Old 11-12-2009 , 23:35   Re: [HELP] Creatting and editting file
Reply With Quote #9

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.
Mxnn 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 13:39.


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