ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
803 stars 128 forks source link

add "special:auto" to FileIO #364

Closed gewang closed 11 months ago

gewang commented 1 year ago

from ChucK discord:

I'm writing a logging feature for a project to dump event data to .csv using FileIO. I thought I could use the same naming conventions/approach we use for WvOut to autogenerate date-time, i.e. "special:auto" but I guess that's specific to WvOut? or am I missing something.

As a workaround I thought I'd pipe datetime from the terminal in as an argument [like (date '+%Y-%m-%d_%R:%S') | chuck myscript.ck], a la @P-Ray's-Cafe Take It For Granite, but I can't find a version (I'm sure I have 30 copies on old hard-drives) or remember how to then read that data within the ChucK script (did it use StringTokenizer?). Any examples of the classic Pe hack floating around?

I guess as a second workaround I could wrap the whole call in a shell script and pass the computed datetime val in as a standard chuck arg but that feels like a lot of work tonight...

thanks!

gewang commented 11 months ago

this has been added will be part of the 1.5.1.5 release

// instantiate a file IO object
FileIO fout;

// optional: set prefix and extension
// (defaults prefix: "chuck-file" ext: "txt")
fout.autoPrefixExtension( "chuck-file", "txt" );

// open for write (default mode: ASCII)
fout.open( "special:auto", FileIO.WRITE );

// test
if( !fout.good() )
{
    cherr <= "can't open file for writing..." <= IO.newline();
    me.exit();
}

// write some stuff
fout <= 1 <= " " <= 2 <= " " <= "foo" <= IO.newline();

// print the file that was written
// FYI before we close it as that clears the filename
<<< "file written:", fout.filename() >>>;

// close the thing
fout.close();