MATLAB: Fopen sometimes not writing all message and saved

fopen

Hi everyone!!!
I have SDP for encrypted and decrypted message but sometimes not save all encrypted message.
for example:
S is encrypted message.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S=`@ `!@ AA C@@@ACCACACCAEAEEAAEEEIIEAIAAYIYAIIAAAAaaAqaAaaAaA!aA!!AA
fh=fopen('tower.txt','w');
fprintf(fh, '%s' ,S);
fclose(fh);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
but tower.txt file has just
`@
please tell me
why not save all?
and how to solve it?
thank you

Best Answer

fprintf() stops at NUL characters inside strings. That is, it stops at characters with value binary 0. This has to do with the fact that the underlying libraries treat binary 0 as the marker for end of string (this is a design that goes back numerous decades.)
You should not use fprintf() for what you are doing.
fwrite(fh, S);