フィルターのクリア

fwrite with fixed length of different data types

4 ビュー (過去 30 日間)
Kanchibhotla Chandra Sekhar
Kanchibhotla Chandra Sekhar 2018 年 12 月 26 日
コメント済み: Walter Roberson 2019 年 1 月 1 日
I have a know format file with different reading format. Example -
fread(fid,[1, 40], 'char')
fread(fid,[1, 4], 'long')
fread(fid,[1, 1], 'short')
% These are data blocks
fread(fid,[400 1],'double')
fread(fid,[400 1],'int')
fread(fid,[400 1],'float')
Now, i have to write the data in the same fashion
fwrite(fid,strwrite,'40*char')
something like that -
how can i pad and fwrite of other data structures

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 12 月 26 日
40*char is not a permitted fwrite precision .
The precision option is not intended to give information about how many elements are being written: instead it gives information about how each input element is to be represented as output. The number is determined by the size of the variable .
fwrite(fid,strwrite,'char')
If you need to pad then fwrite the pad values yourself .
fwrite(zeroes(1,7),'uint8')
for example .
It is not possible to fwrite two different datatypes in one call. (However memory mapping routines can input or output entire structure at a time .)
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 12 月 31 日
no, pad(nodestr,16) would pad adding 16 values along the first dimension leading to a 17 x something character array.
Also C char is not fixed width: it is the smallest integer datatype supported by the target that is at least 8 bits and is implementation dependent as to whether it is signed or not. You should avoid defining interfaces in terms of char . Traditional C up to C89 did not offer any explicitly 8 bit datatype .
MATLAB fwrite char precision uses as many bytes as needed to encode the unicode input as the destination encoding specified or defaulted at fopen time. char*1 is 8 bit and will saturate for input beyond 255. C's char is not "wide" character .
Walter Roberson
Walter Roberson 2019 年 1 月 1 日
Some time ago I wrote a small function to fwrite() with padding or truncation to a given length. It is easy to do and makes it easier to write out fixed-width structures.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by