POV-Ray : Newsgroups : povray.general : File handles for output streams : Re: File handles for output streams Server Time
29 Jun 2025 12:48:03 EDT (-0400)
  Re: File handles for output streams  
From: kurtz le pirate
Date: 20 Jun 2025 11:03:10
Message: <6855782e$1@news.povray.org>
On 19/06/2025 18:24, Cousin Ricky wrote:
> On 2025-06-19 09:39 (-4), Chris R wrote:
>>
>> Follow-up question: it appears you can't pass file handles as parameters to
>> macros.  No tricks for that either?
> 
> Here's my workaround:
> 
> ---%<-----%<-----%<---[BEGIN CODE]---%<-----%<-----%<---
> #macro Write_something (Filename, N)
>     #fopen F Filename append
>     #write (F, N)
>     #fclose F
> #end
> 
> #declare FILE1 = "test.txt"
> #fopen F FILE1 write #fclose F //zero the output file
> Write_something (FILE1, "Hello,\n")
> Write_something (FILE1, "world.\n")
> --->%----->%----->%----[END CODE]---->%----->%----->%---
> 


Ingenious approach. I had started with the logic of opening all my files 
and choosing to write in this one or that one. I closed all my files at 
the end.


With your clever solution, when you write to a file, you open and close 
it each time.


Below is a stupid code that illustrates your concept and works really 
well with multiple files.

// ---------------------------------------------------------------------
#declare odd  = function(x) {select(mod(x, 2), 1, 0, 1)} // no include

// ---------------------------------------------------------------------
#macro Write_something (Filename, N)
    #fopen F Filename append
    #write (F, concat(str(N,0,0),"\n"))
    #fclose F
#end

// ---------------------------------------------------------------------
#macro Create_file(Filename)
   #fopen F Filename write
   #fclose F
#end


// ---------------------------------------------------------------------
#declare N = 5;
#declare Files = array[N]
#declare i = 0;
#while ( i < N )
   #declare Files[i] = concat("file",str(i,0,0),".txt");
   Create_file(Files[i])
   #declare i = i +1;
#end

#declare i = 0;
#while ( i < 25 )
   Write_something(Files[mod(i,N)],i)
   #declare i = i + 1;
#end

#error "Nothing to draw"
// ---------------------------------------------------------------------




Thanks for your tip ;)

-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.