|
|
Le_Forgeron <jgr### [at] freefr> wrote:
> isn't *file_exists(S)* part of the solution ?
>
> http://wiki.povray.org/content/Reference:Numeric_Expressions
Indeed, my friend, it is.
file_exists(S)
Attempts to open the file specified by the string S. The current directory and
all library directories specified by the Library_Path or +L options are also
searched. See Library Paths for details. Returns 1 if successful and 0 if
unsuccessful.
Although I suspected this existed, I did not think to look under "Numeric
Expressions" - I was looking under #read, #write, #append, etc.
I was hoping that there would be a similar example to
#fopen FILE_HANDLE_IDENTIFIER "mydata.txt" read
#while (defined(FILE_HANDLE_IDENTIFIER))
#read (FILE_HANDLE_IDENTIFIER,Var1,Var2,Var3)
...
#end
for the file commands, but alas, no.
So here ya go:
############################################################################
#version 3.7;
global_settings {assumed_gamma 1.0}
#include "debug.inc"
Set_Debug (true)
#declare FILENAME = "TestFile.txt";
//#declare FILE_HANDLE_IDENTIFIER = 1;
#declare ONE = 0; #declare TWO = 0; #declare THREE = 0; #declare FOUR = 0;
#declare FIVE = 0;
#declare Existence = file_exists (FILENAME);
// Attempts to open the file specified by the string S.
// The current directory and all library directories specified by the
Library_Path or +L options are also searched.
// See Library Paths for details. Returns 1 if successful and 0 if unsuccessful.
#debug "\n"
#debug
"#######################################################################################################\n"
#debug concat("Checking for existence of file ", FILENAME, " ... \n")
#declare Existence = file_exists (FILENAME);
#if (Existence = 1)
#debug concat ("Directive 'file_exists (FILENAME)' = ", str (Existence, 1, 1),
" File ", FILENAME, " exists. \n")
#else
#debug concat ("Directive 'file_exists (FILENAME)' = ", str (Existence, 1, 1),
" File ", FILENAME, " does not exist. \n")
#end
#debug "\n"
//---------------------------------------------------------------------------------------------------------------------
----------
#debug concat("Opening file ", FILENAME, " for writing ... \n")
#fopen FILE_HANDLE_IDENTIFIER FILENAME write
#debug concat("Writing data to file ... \n")
#write (FILE_HANDLE_IDENTIFIER, 1, ", ", 2, ", ", 3, ", ", 4, ", ", 5, ", \n")
#write (FILE_HANDLE_IDENTIFIER, 6, ", ", 7, ", ", 8, ", ", 9, ", ", 10, "\n")
#debug concat("Closing file ... \n")
#fclose FILE_HANDLE_IDENTIFIER
#debug "\n"
//---------------------------------------------------------------------------------------------------------------------
----------
#debug concat("Checking for existence of file ", FILENAME, " ... \n")
#declare Existence = file_exists (FILENAME);
#if (Existence = 1)
#debug concat ("Directive 'file_exists (FILENAME)' = ", str (Existence, 1, 1),
" File ", FILENAME, " exists. \n")
#else
#debug concat ("Directive 'file_exists (FILENAME)' = ", str (Existence, 1, 1),
" File ", FILENAME, " does not exist. \n")
#end
#debug "\n"
//---------------------------------------------------------------------------------------------------------------------
----------
#declare Existence = file_exists (FILENAME);
#if (Existence = 1)
#debug concat("Opening file ", FILENAME, " for reading ... \n")
#debug "\n"
#fopen FILE_HANDLE_IDENTIFIER FILENAME read
// If you attempt to read past end-of-file, the file is automatically closed
and the FILE_HANDLE_IDENTIFIER is deleted from the symbol table.
// This means that the boolean function defined(FILE_HANDLE_IDENTIFIER) can be
used to detect end-of-file.
#while (defined (FILE_HANDLE_IDENTIFIER))
#debug "File open, reading data from file... \n"
#read (FILE_HANDLE_IDENTIFIER, ONE, TWO, THREE, FOUR, FIVE)
// output 5-term color expression data
#declare Vector = <ONE, TWO, THREE, FOUR, FIVE>;
#debug concat( "Vector.x = ", str(Vector.x, 3, 0), "\n")
#debug concat( "Vector.u = ", str(Vector.u, 3, 0), "\n\n")
#debug concat( "Vector.y = ", str(Vector.y, 3, 0), "\n")
#debug concat( "Vector.v = ", str(Vector.v, 3, 0), "\n\n")
#debug concat( "Vector.z = ", str(Vector.z, 3, 0), "\n\n")
#debug concat( "Vector.t = ", str(Vector.t, 3, 0), "\n")
#debug concat( "Vector.filter = ", str(Vector.filter, 3, 0), "\n\n")
#debug concat( "Vector.transmit = ", str(Vector.transmit, 3, 0), "\n\n")
#end
#debug "End of file (EOF) reached. File automatically closed. \n"
#fclose FILE_HANDLE_IDENTIFIER // just to make sure
#else
#debug "File ", FILENAME, " does not exist. Cannot open for reading. \n"
#end // end if
#debug
"#######################################################################################################\n"
#debug "\n"
Post a reply to this message
Attachments:
Download 'filecommands.pov.txt' (4 KB)
|
|