Reading ints from a file in Chuck
I have this ChucK code:
"examples/vento.txt" => string filename; FileIO fio; // open a file fio.open(filename, FileIO.READ); // ensure it ok if(!fio.good()) { cherr <= "can't open file: " <= filename <= " for reading..." <= IO.newline(); me.exit(); } fio.readLine() => string velocity; fio.readLine() => string direction; The text file contains:
10 12 (updated with python every minute)
I want to convert speed and direction to int (or better float).
How can i do this?
Use atoi and atof in the Std library. Let's say you want to transfer from 0-127 (MIDI speed) to a float from 0 to 1.0 (which is much more convenient for unit generators):
Std.atoi(fio.readLine()) => int midi_velocity; midi_velocity/127.0 => float velocity; <<< velocity >>>; should print 0.078740 :(float) for input 10.
Or, if you just want to go swimming:
Std.atof(fio.readLine()) => float velocity; <<< velocity >>>; which prints 10.000000 :(float) .