Raised This Month: $ Target: $400
 0% 

Remove a line in a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Deviance
Veteran Member
Join Date: Nov 2004
Location: Sweden
Old 01-06-2007 , 16:43   Remove a line in a file
Reply With Quote #1

I'm having a hard time to get this one. Say that i got a file whit thease lines
Code:
test1
test2
test3
test4
... And i want to remove test3, so there is only test1, test2, and test4 in the file. I have tried to replace the line whit an empty line, but the server crashes when i do so. Please tell me how to do this, it would be really useful!
Deviance is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 01-06-2007 , 18:30   Re: Remove a line in a file
Reply With Quote #2

Do you want an empty line or to remove the line completely?

Last edited by [ --<-@ ] Black Rose; 01-06-2007 at 18:36.
[ --<-@ ] Black Rose is offline
MaximusBrood
Veteran Member
Join Date: Sep 2005
Location: The Netherlands
Old 01-06-2007 , 18:43   Re: Remove a line in a file
Reply With Quote #3

The real content of your text file is this:

test1\ntest2\ntest3\ntest4

If you want to remove the test3 line, remove test3\n
Show your crashing code please.
__________________
Released six formerly private plugins. Not active here since ages.
MaximusBrood is offline
Deviance
Veteran Member
Join Date: Nov 2004
Location: Sweden
Old 01-06-2007 , 18:51   Re: Remove a line in a file
Reply With Quote #4

Quote:
Originally Posted by MaximusBrood View Post
The real content of your text file is this:

test1\ntest2\ntest3\ntest4

If you want to remove the test3 line, remove test3\n
Show your crashing code please.
Something like
Code:
new file = fopen(filename,"wt+") new text[64], text_to_find[] = "lololol" while(!feof(file)) {     fgets(file, text, 63)     if(contain(text, text_to_find) != -1)     {         fprintf(file, " ")         break     } } fclose(file)
This isn't exacly the crash code, but it's the same way.

Last edited by Deviance; 01-07-2007 at 07:44.
Deviance is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 01-06-2007 , 19:47   Re: Remove a line in a file
Reply With Quote #5

It's not crashing, it's going infinite.

Last edited by [ --<-@ ] Black Rose; 01-07-2007 at 12:44.
[ --<-@ ] Black Rose is offline
Deviance
Veteran Member
Join Date: Nov 2004
Location: Sweden
Old 01-07-2007 , 03:12   Re: Remove a line in a file
Reply With Quote #6

Quote:
Originally Posted by [ --<-@ ] Black Rose View Post
It's not crashing, it's going infinite.
It stops reading the file if it finds the line that it is looking after, see the break?
Deviance is offline
teame06
i have a hat
Join Date: Feb 2005
Location: Hat City
Old 01-07-2007 , 13:10   Re: Remove a line in a file
Reply With Quote #7

Quote:
Originally Posted by p3tsin
Those are the modes you can use with fopen.
__________________
No private support via Instant Message
GunGame:SM Released
teame06 is offline
Send a message via AIM to teame06
Deviance
Veteran Member
Join Date: Nov 2004
Location: Sweden
Old 01-07-2007 , 15:43   Re: Remove a line in a file
Reply With Quote #8

This still doesn't solve my problem, is there any other way to remove a line?
Deviance is offline
schnitzelmaker
Senior Member
Join Date: Apr 2006
Location: HERE
Old 01-07-2007 , 22:27   Re: Remove a line in a file
Reply With Quote #9

A way is making a copy of that file without that line and rename the copy to it.Here an example:
Code:
#include <amxmodx> #include <amxmisc> #define PLUGIN "New Plug-In" #define VERSION "1.0" #define AUTHOR "Administrator" new filename[256],filename2[256] public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR)     get_configsdir(filename,255)     get_configsdir(filename2,255)     format(filename,255,"%s/test.txt",filename)     format(filename2,255,"%s/test2.txt",filename2) } //I have use this only for testing public client_putinserver() {     test() } public test() {     new file = fopen(filename,"r")     new tmpfile = fopen(filename2,"a+")     new text[64], text_to_find[] = "hello"         if(!file || !tmpfile)         console_print(0,"Error:%d,%d",file,tmpfile)             while(!feof(file))     {         fgets(file, text, 63)         if(contain(text, text_to_find) != -1)             continue         fputs(tmpfile,text)     }         fclose(file)     fclose(tmpfile)     //delete file or rename not work     delete_file(filename)         //check if rename return an error(0)     if (!rename_file (filename2, filename,1))         console_print(0,"Error on rename file:%s->%s",filename2,filename)     else delete_file(filename2) }
Or using the "write_file ( const file[], const text[], [ line ] )" command,that use the same method,but you can only replace with an empty line,not complete delete the line.
__________________

Last edited by schnitzelmaker; 01-07-2007 at 22:34.
schnitzelmaker is offline
hjvl
Member
Join Date: Oct 2005
Location: Almere
Old 03-23-2007 , 04:39   Re: Remove a line in a file
Reply With Quote #10

Could this be something ?
first count the lines and use the "linecount" in your loop
hjvl is offline
Reply



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:22.


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