Home All Groups Group Topic Archive Search About

Access Single Line in Text File

Author
10 May 2005 11:31 PM
Ole Weigelt
Hello Everybody,

is it possible to change a single line in  a text file?

Open "File" For Append   only lets you insert at the end
Open "File" For Binary  has the problem that you never know how long a line
actually is. Therefore the is no command like Line Input

Unfortunately, now my wisdom ends...

Thanks for reading

Ole Weigelt

Author
10 May 2005 11:50 PM
Duane Bozarth
Ole Weigelt wrote:
>
> Hello Everybody,
>
> is it possible to change a single line in  a text file?
>
> Open "File" For Append   only lets you insert at the end
> Open "File" For Binary  has the problem that you never know how long a line
> actually is. Therefore the is no command like Line Input
>
> Unfortunately, now my wisdom ends...
>

If it isn't fixed-length records, then there really isn't a simple
way...if the file is relatively small it can be read to memory and
manipulated there, then rewritten as one way.
Author
11 May 2005 12:26 AM
Ole Weigelt
Show quote
"Duane Bozarth" <dpboza***@swko.dot.net> schrieb im Newsbeitrag
news:428148D6.B87B49CF@swko.dot.net...
> Ole Weigelt wrote:
> >
> > Hello Everybody,
> >
> > is it possible to change a single line in  a text file?
> >
> > Open "File" For Append   only lets you insert at the end
> > Open "File" For Binary  has the problem that you never know how long a
line
> > actually is. Therefore the is no command like Line Input
> >
> > Unfortunately, now my wisdom ends...
> >
>
> If it isn't fixed-length records, then there really isn't a simple
> way...


if the file is relatively small it can be read to memory and
> manipulated there, then rewritten as one way.

That was what I wanted to avoid.

But I thought that it is not that simple.

Thanks

Ole
Author
11 May 2005 1:53 AM
Duane Bozarth
Ole Weigelt wrote:
Show quote
>
> "Duane Bozarth" <dpboza***@swko.dot.net> schrieb im Newsbeitrag
> news:428148D6.B87B49CF@swko.dot.net...
> > Ole Weigelt wrote:
> > >
> > > Hello Everybody,
> > >
> > > is it possible to change a single line in  a text file?
> > >
> > > Open "File" For Append   only lets you insert at the end
> > > Open "File" For Binary  has the problem that you never know how long a
> line
> > > actually is. Therefore the is no command like Line Input
> > >
> > > Unfortunately, now my wisdom ends...
> > >
> >
> > If it isn't fixed-length records, then there really isn't a simple
> > way...
>
> if the file is relatively small it can be read to memory and
> > manipulated there, then rewritten as one way.
>
> That was what I wanted to avoid.
>
> But I thought that it is not that simple.
....

But, I pointed out the "all in memory" option as sometimes posters don't
think of it....

Note also that if you change the length of the line in the manipulation,
you'll have to rewrite the file from that point on anyway...
Author
11 May 2005 1:46 PM
Peter Aitken
Show quote
"Ole Weigelt" <o.weigelt@officeworks> wrote in message
news:OqmdIAcVFHA.3808@TK2MSFTNGP14.phx.gbl...
>
> "Duane Bozarth" <dpboza***@swko.dot.net> schrieb im Newsbeitrag
> news:428148D6.B87B49CF@swko.dot.net...
>> Ole Weigelt wrote:
>> >
>> > Hello Everybody,
>> >
>> > is it possible to change a single line in  a text file?
>> >
>> > Open "File" For Append   only lets you insert at the end
>> > Open "File" For Binary  has the problem that you never know how long a
> line
>> > actually is. Therefore the is no command like Line Input
>> >
>> > Unfortunately, now my wisdom ends...
>> >
>>
>> If it isn't fixed-length records, then there really isn't a simple
>> way...
>
>
> if the file is relatively small it can be read to memory and
>> manipulated there, then rewritten as one way.
>
> That was what I wanted to avoid.
>
> But I thought that it is not that simple.
>
> Thanks
>
> Ole
>

You can use Line Input to read each line in and write it to a temp file.
When you find the line you want to change, change it and write it. Then read
in all remaining lines and write to the temp file. Finally delete the
original file and rename the temp file. Sort of clunky but I cannot think of
a better way that does not involve having the whole file in memory.


--
Peter Aitken

Remove the crap from my email address before using.
Author
11 May 2005 8:24 PM
Ole Weigelt
Show quote
"Peter Aitken" <pait***@CRAPnc.rr.com> schrieb im Newsbeitrag
news:OKWAK$iVFHA.3572@TK2MSFTNGP12.phx.gbl...
> "Ole Weigelt" <o.weigelt@officeworks> wrote in message
> news:OqmdIAcVFHA.3808@TK2MSFTNGP14.phx.gbl...
> >
> > "Duane Bozarth" <dpboza***@swko.dot.net> schrieb im Newsbeitrag
> > news:428148D6.B87B49CF@swko.dot.net...
> >> Ole Weigelt wrote:
> >> >
> >> > Hello Everybody,
> >> >
> >> > is it possible to change a single line in  a text file?
> >> >
> >> > Open "File" For Append   only lets you insert at the end
> >> > Open "File" For Binary  has the problem that you never know how long
a
> > line
> >> > actually is. Therefore the is no command like Line Input
> >> >
> >> > Unfortunately, now my wisdom ends...
> >> >
> >>
> >> If it isn't fixed-length records, then there really isn't a simple
> >> way...
> >
> >
> > if the file is relatively small it can be read to memory and
> >> manipulated there, then rewritten as one way.
> >
> > That was what I wanted to avoid.
> >
> > But I thought that it is not that simple.
> >
> > Thanks
> >
> > Ole
> >
>
> You can use Line Input to read each line in and write it to a temp file.
> When you find the line you want to change, change it and write it. Then
read
> in all remaining lines and write to the temp file. Finally delete the
> original file and rename the temp file. Sort of clunky but I cannot think
of
> a better way that does not involve having the whole file in memory.
>

This is exactly what I do now. I was hoping to find a nicer solution, but
now I will keep it that way

Thanks to both of you for your help!!

Show quote
>
> --
> Peter Aitken
>
> Remove the crap from my email address before using.
>
>

AddThis Social Bookmark Button