inserting stuff at beginning of a stream

View: New views
4 Messages — Rating Filter:   Alert me  

inserting stuff at beginning of a stream

by yosato :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I almost know this is an elementary question, but as I seem to
struggle... I wonder how one could `push' stuff to the *beginning* of a
file in SWI. Appending stuff at the end of a stream is easy, with

open(File, append, ID), write(ID, stuff).

But how do I insert stuff at the beginning? I tried

open(File, update, ID), write(ID, stuff)

and realised this *replaces* the beginning of the File with stuff, rather
than inserting it (and pushing the File content after it).

Advice would be appreciated.

Yo

_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: inserting stuff at beginning of a stream

by Nicolas Pelletier-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

On Mon, Oct 26, 2009 at 1:14 AM, Yo Sato <yosato16@...> wrote:

>
> I almost know this is an elementary question, but as I seem to
> struggle... I wonder how one could `push' stuff to the *beginning* of a
> file in SWI. Appending stuff at the end of a stream is easy, with
>
> open(File, append, ID), write(ID, stuff).
>
> But how do I insert stuff at the beginning? I tried
>
> open(File, update, ID), write(ID, stuff)
>
> and realised this *replaces* the beginning of the File with stuff, rather
> than inserting it (and pushing the File content after it).

Did you try writing to a new file, as in:

open(New_file, write, Out_stream),
write(Out_stream, Stuff),
open(File, read, In_stream),
copy_stream_data(In_stream, Out_stream)... % This uses put_code/2

HTH,

--
Nicolas
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Parent Message unknown Re: inserting stuff at beginning of a stream

by yosato :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Nicolas, this seems to work fine ('copy_stream_data/2' is very
useful). I guess there's no escaping using some tmp file. My tentative
solution is as below, which seems still rather ugly and fraught with
overhead..

% given the original file is named 'file2renew'
open(tmp, write, Tmp),
write(Tmp, newstuff),
open(file2renew, read, File2Renew),
copy_stream_data(File2Renew,Tmp),
close(Tmp),close(File2Renew),
rename_file(tmp,file2Renew),
delete_file(tmp).

If there is a better solution (which I think is very likely), where one
may perhaps not need a temp file or swapping files etc, please advise.

Yo

On Tue, 27 Oct 2009 09:01:34 +0900, Nicolas Pelletier wrote:

> Hello,
>
> On Mon, Oct 26, 2009 at 1:14 AM, Yo Sato <yosato16@...> wrote:
>>
>> I almost know this is an elementary question, but as I seem to
>> struggle... I wonder how one could `push' stuff to the *beginning* of a
>> file in SWI. Appending stuff at the end of a stream is easy, with
>>
>> open(File, append, ID), write(ID, stuff).
>>
>> But how do I insert stuff at the beginning? I tried
>>
>> open(File, update, ID), write(ID, stuff)
>>
>> and realised this *replaces* the beginning of the File with stuff,
>> rather than inserting it (and pushing the File content after it).
>
> Did you try writing to a new file, as in:
>
> open(New_file, write, Out_stream),
> write(Out_stream, Stuff),
> open(File, read, In_stream),
> copy_stream_data(In_stream, Out_stream)... % This uses put_code/2
>
> HTH,


_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: Re: inserting stuff at beginning of a stream

by jeffrose :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yo:

There is perhaps another alternative. SWI-Prolog provides a "memory file" library that is roughly equivalent
to Smalltalk's ReadWriteStream class. You can create a memory-based virtual file that implements the stream interface. If your
stuff is small, this may be the way to go.

Example is attached.

Regards,
Jeff R.


-----Original Message-----
From: Yo Sato <yosato16@...>
To: swi-prolog@...
Sent: Fri, Oct 30, 2009 9:18 am
Subject: [SWIPL] Re: inserting stuff at beginning of a stream

Thanks Nicolas, this seems to work fine ('copy_stream_data/2' is very 
useful). I guess there's no escaping using some tmp file. My tentative
solution is as below, which seems still rather ugly and fraught with
overhead..

% given the original file is named 'file2renew'
open(tmp, write, Tmp),
write(Tmp, newstuff),
open(file2renew, read, File2Renew),
copy_stream_data(File2Renew,Tmp),
close(Tmp),close(File2Renew),
rename_file(tmp,file2Renew),
delete_file(tmp).

If there is a better solution (which I think is very likely), where one
may perhaps not need a temp file or swapping files etc, please advise.

Yo

On Tue, 27 Oct 2009 09:01:34 +0900, Nicolas Pelletier wrote:

> Hello,
>
> On Mon, Oct 26, 2009 at 1:14 AM, Yo Sato <yosato16@...> wrote:
>>
>> I almost know this is an elementary question, but as I seem to
>> struggle... I wonder how one could `push' stuff to the *beginning* of a
>> file in SWI. Appending stuff at the end of a stream is easy, with
>>
>> open(File, append, ID), write(ID, stuff).
>>
>> But how do I insert stuff at the beginning? I tried
>>
>> open(File, update, ID), write(ID, stuff)
>>
>> and realised this *replaces* the beginning of the File with stuff,
>> rather than inserting it (and pushing the File content after it).
>
> Did you try writing to a new file, as in:
>
> open(New_file, write, Out_stream),
> write(Out_stream, Stuff),
> open(File, read, In_stream),
> copy_stream_data(In_stream, Out_stream)... % This uses put_code/2
>
> HTH,


_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog


cat_stream.pl (998 bytes) Download Attachment