Re: {Spam?} writing binary files
H William Wellliver III <[email protected]> Mon, 01 May 2017 13:08:21 -0400
| Newsgroups | gmane.comp.lang.pike.user |
|---|---|
| Message-ID | <[email protected]> |
Something like this should work (though is probably not the most
efficient approach):
Stdio.File f = Stdio.File("foo.bin", "wct");
array arr = ({ 4, 5, 9, 12, 255, 0, 8 });
for(int i = 0; i < sizeof(arr); i++)
f->write(String.int2char(arr[i]));
f->close();
Stdio.read_file("foo.bin");
(2) Result: "\4\5\t\f\377\0\b"
You can probably find a use of sprintf() that can convert the whole int
array into a string of chars in one go... look at the [] operators in
the sprintf() documentation.
Hope this helps!
Bill
On 2017-05-01 05:52, larcky wrote:
> Hello
> Is Pike able to write binary files? Something like this in C:
>
> #include <stdio.h>
> int main()
> {
> int n = 7;
> int arr[] = { 4, 5, 9, 12, 255, 0, 8 };
> FILE *fout = fopen("foo.bin", "wctb");
>
> for (int i = 0; i < n; ++i) {
> fwrite(arr+i, 1, 1, fout);
> }
> fclose(fout);
> return 0;
> }
>
> Thanks.
>