AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Remove new line character from file (https://forums.alliedmods.net/showthread.php?t=89895)

Sn!ff3r 04-11-2009 17:58

Remove new line character from file
 
As title - how to remove "enter" aka "^n" from file?

danielkza 04-11-2009 18:00

Re: Remove new line character from file
 
You mean strip all new-lines or just the final/starting one? What kind of file is it?

Sn!ff3r 04-11-2009 18:03

Re: Remove new line character from file
 
Hm. Maybe differently. How to permanently remove line from file ?

PHP Code:

public concmd_delete(id,level,cid)
{
    if(!
cmd_access(id,level,cid,2))
        return 
PLUGIN_HANDLED
    
    
static args[33]
    
read_args(args,32)
    
    if(!
strlen(args))
        return 
PLUGIN_HANDLED
    
    
new handler fopen("file.txt""rwt+")
    
    static 
buffer[128]
    while(!
feof(handler))
    {
        
fgets(handlerbuffersizeof buffer 1)
        
        if(
equal/*i-*/(buffer,args))
        {
            
//fprintf(handler, "") // BAD!
            // here i want to remove this line
            
break
        }
    }
    
fclose(handler)    
    return 
PLUGIN_HANDLED



stupok 04-11-2009 20:47

Re: Remove new line character from file
 
Well, afaik, there are two ways.

1. Read lines from file 1 and write them to file 2, skip the deleted lines.
2. Read file 1 into a buffer, remove lines, write to file 2.

If you need code, I can show you.

Usually, I just do write_file("file", "", line). It's shorter code and I don't really care about extra empty lines.

Bugsy 04-11-2009 23:17

Re: Remove new line character from file
 
As long as the file isn't huge and you are concerned with only removing the carriage returns and not any other data.

1. Read entire file into a buffer
2. Replace all carriage returns with ""
3. Delete original file, create new file

Exolent[jNr] 04-12-2009 00:12

Re: Remove new line character from file
 
I think stupok's way would be best.

Code:
new f = fopen("yourfile.txt", "rt"); new line; new bool:delete; new data[/* your data size */]; while( !feof(f) ) {     fgets(f, data, sizeof(data) - 1);         if( /* this is the line you want to delete */ )     {         delete = true;         break;     }         line++; } fclose(f); if( delete )     write_file("yourfile.txt", "", line);

That will make the line blank, and shouldn't be a problem when you need to read data.
Just make a "if( !data[0] ) continue;" type check.

Bugsy 04-12-2009 00:22

Re: Remove new line character from file
 
Right, for removing an entire line. He originally just requested how to remove the carriage return (not the data following it), which is not what stupoks methods suggests.

Exolent[jNr] 04-12-2009 00:26

Re: Remove new line character from file
 
I'm not quite sure what he's asking for.
Does he want to delete an entire line, or the new line character from the end of the line, causing 2 lines to combine?

Bugsy 04-12-2009 00:42

Re: Remove new line character from file
 
Quote:

Originally Posted by Exolent[jNr] (Post 803426)
I'm not quite sure what he's asking for.
Does he want to delete an entire line, or the new line character from the end of the line, causing 2 lines to combine?

That's what I wasn't sure about, his request is a bit vague.

stupok 04-12-2009 14:26

Re: Remove new line character from file
 
Quote:

Originally Posted by Sn!ff3r (Post 803275)
How to permanently remove line from file ?
PHP Code:

//fprintf(handler, "") // BAD!
// here i want to remove this line 


I think it's pretty clear that he wants to remove empty lines. Empty lines are caused by the lazy delete method mentioned in my post.


All times are GMT -4. The time now is 02:25.

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