Re: files...

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Thu, 23 Dec 1999, MiCkey wrote:
> >%_Is it possible to:
>
> 1º) delete a line (or more) of a file without copy all the file except the
> lines I want to delete?
>
> 2º) read, ie, 10 lines of a file using gets() and after return to the line 1
> without have to close and open the file again?
>
> Thanks,
> Luís Fernando

If you mean a text file containing random-length lines, the only way is to
read the data in, remove the lines, and write the data back to the file.
This is true of any language that can write "text" files.

You can do this one line at a time, reading the old file and writing to a new
file. If the file is not larger than the memory in your computer, then you
can read the entire file into a sequence, remove lines from the sequence,
and write the entire file back to disk. This is much faster than the one-line
at a time method.

sequence s
object line
integer fn

s = {}
fn = open("dbase.html","r") -- read mode
while 1 do
   line = gets(fn)
   if atom(line) then exit -- EOF
   else s = append(s,line)
   end if
end while
close(fn)

-- s will contain the lines: s[1] is line 1, s[20] is line 20.....
-- do your deletions here, the write the lines back using a
-- loop:

fn = open("dbase.html","w") -- write mode
for i = 1 to length(s) do
 puts(fn,s[i]])
end for
close(fn)

Regards,
Irv

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu