Effacer les filtres
Effacer les filtres

fwrite

3 vues (au cours des 30 derniers jours)
Tor Fredrik Hove
Tor Fredrik Hove le 18 Nov 2011
I tried to use fwrite which is described as saving in binary form but it saved it in numbers. At least my attempt did:
Created a new file rain2.tv and atached too numbers to it and read it afterwards:
>> fid=fopen('rain2.tv','w')
fid =
3
>> fwrite(fid, '2 4')
ans =
3
When i open the file rain2.tv it is in normal numbers not binary and when I look at the saved file it is also saved in normal

Réponse acceptée

Titus Edelhofer
Titus Edelhofer le 18 Nov 2011
Hi Tor,
1) you save strings 2,4, not values (pass [2 4] instead of '2 4' to fwrite
2) you should pass the format ('double'), see fwrite doc
Titus
  2 commentaires
Tor Fredrik Hove
Tor Fredrik Hove le 18 Nov 2011
I have read doc fwrite but I can't seem t oget a specific example. I have written:
>> fid=fopen('rain2.tv','w')
fid =
3
>> fwrite(fid, [1 2])
ans =
2
>> fclose(fid)
ans =
0
>> fopen('rain2.tv')
ans =
3
>> while ~feof(fid)
fgetl(fid)
end
ans =
>>
the ans is empty and noting is saved in the file
Walter Roberson
Walter Roberson le 18 Nov 2011
The ans is not empty: I can see from what you posted that it contains two characters, binary 1 and binary 2.
fgetl() is used for reading *text*, not for reading binary. You would use fread() to read binary.
fread(fid)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Low-Level File I/O dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by