Delete 1 line in file
#1

Hi I've a big problem i can't create this function :/ fdeleteline by Sacky doesn't work so not give me this
maybe u have your own function?
Reply
#2

How you want delete line? By line number, or what else?
Reply
#3

yes or text, I've this in file for example
Code:
sd
afd
hgfgh
gfjg
34v3g
4g234
jgdfg
ghj
and I want delete one of this
Reply
#4

To remove a line by line number, you can use something like this (no it isn't Sacky's function, i have just writted it now! and work):
pawn Code:
fdeleteline(filename[], line)
{
  new count, string[256], File:file, File:temp;

  file= fopen(filename, io_read);
  temp = fopen("tmpfile.tmp", io_write);

  while (fread(file, string))
    if (++count != line)
      fwrite(temp, string);

  fclose(file);
  fclose(temp);

  file= fopen(filename, io_write);
  temp = fopen("tmpfile.tmp", io_read);

  while (fread(temp, string))
    fwrite(file, string);
   
  fclose(file);
  fclose(temp);
  fremove("tmpfile.tmp");
}

If your file is file.txt, and it contain this:
Code:
sd
afd
hgfgh
gfjg
if you do
pawn Code:
fdeleteline("file.txt", 3);
then file.txt will now contain
Code:
sd
afd
gfjg
Reply
#5

Or read the entire file to a buffer, Use strreplace, Then write it back to the file
Reply
#6

I have this

Code:
stock fdeleteline(filename[], dest[], remove[])
{
  new string[255], File:handle, File:ftmp;

  handle = fopen(filename, io_read);
  ftmp = fopen(dest, io_write);
  while(fread(handle, string)){
    for(new i = 0, j = strlen(string); i < j; i++) if(string[i] == '\n' || string[i] == '\r') string[i] = '\0';
 		if(strcmp(string, remove, false) != 0) fwrite(ftmp, string);
  }
  fclose(handle);
  fclose(ftmp);
  handle = fopen(filename, io_write);
  ftmp = fopen(dest, io_read);
  while(fread(ftmp, string)){
  for(new i = 0, j = strlen(string); i < j; i++) if(string[i] == '\n' || string[i] == '\r') string[i] = '\0';
	fwrite(handle, string);
	}
  fclose(handle);
  fclose(ftmp);
  fremove(dest);
}
but this write

Code:
sdf sffsfa dgsd dhfh dghfh
without spaces and I want that

Code:
sdf
sffsfa
dgsd
dhgfh
Reply
#7

You remove newline characters so..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)