Write char[32] in binary file, but string may be shorter

Dear,

I want to reserve in a binary file a part for max 32 characters.

In Scilab I tried:

myStr = blanks(32);
myStr(1:4) = ‘test’; // expected ‘test’ and the rest remaining blanks
// but myStr became ‘test’ only

So, if “myStr” is shorter than 32 characters, I guess I need to fill up the remaining places with blanks.

I need to write exactly 32 characters into the output binary file, because if this is loaded in another software, it expects exactly 32 characters.

Writing the string into the binary file I currently use:

fd = mopen(fileNmae, “wb”);
mputstr( myStr, fd);
mclose(fd);

Question 1: Is this the correct procedure?
Question 2: Is there something like mputstr(n, myStr, fd);

with n beeing the nr of chars to write and
if length(myStr) < n the remaining chars would be filled with blanks ?

Thank you,
Philipp

This will work in Matlab, which considers strings as arrays of chars, not in Scilab, where you get

--> myStr(1:4) = 'test'
 myStr  = 

  "test"
  "test"
  "test"
  "test"

I woul suggest using mfprintf instead:

mfprintf(fd,"%-32s",myStr)

using standard output confirms the actual length of the string output:

--> myStr=msprintf("%-32s","test")
 myStr  = 

  "test                            "

--> length(msprintf("%-32s","test"))
 ans  =

   32.

Works like charm.
Thank you very much.