File: write-byte.ck

package info (click to toggle)
chuck 1.5.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 41,260 kB
  • sloc: cpp: 124,539; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (35 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//---------------------------------------------------------------------
// name: write-byte.ck
// desc: example of writing bytes in BINARY mode
//---------------------------------------------------------------------
// filename
"write-byte.txt" => string filename;
// instantiate a file IO object
FileIO fout;
// open for write (in BINARY mode)
fout.open( filename, FileIO.WRITE | FileIO.BINARY );

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

// byte sequence to write; equivalent to:
// abc\n123\n
[97, 98, 99, 10, 49, 50, 51, 10] @=> int bytes[];

// loop
for( int i; i < bytes.size(); i++ )
{
    // write the next byte
    // (other options include IO.INT16 and IO.INT32)
    fout.write( bytes[i], IO.INT8 );
}

// close the thing
fout.close();

// print
<<< "file written:", filename >>>;