user6901236
user6901236

Reputation:

Write to binary file in Julia

I need to record some data to binary file in Julia. On C I use next code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE* out = fopen( "test2.bin", "wb" );
if( out==NULL )
    return 1;

putc(49,out);
fclose(out);
} 

and got expected result(I look as char in hex-editor):

1

After rewrite the code to Julia,I got the follow code:

out =  open("test.bin","w")
write(out,49)
close(out) 

but result is:

1.......

From documentation I know that function write returns the number of bytes(in my case it 8 but should be 1).

So my questions: 1. What I am doing wrong? 2. How right write to binary file in Julia?

Upvotes: 2

Views: 3069

Answers (1)

user4651282
user4651282

Reputation:

You on right way. The reason is that 8 it is size of Int, so you need used explicit type conversion Int to Char if want write as Char:

write(out,Char(49))

Upvotes: 3

Related Questions