Writing a integer in an existing file

6 Ansichten (letzte 30 Tage)
Roselena Rubino
Roselena Rubino am 3 Nov. 2020
Kommentiert: Roselena Rubino am 11 Nov. 2020
Hello,
I'm trying to overwrite a file with a int16 precission value and I'm not able to do it. I'm usign this code ('x.DBL' is the name of the file):
fp=fopen('x.DBL','r+');
fwrite(fp,dd,'int16'); %dd is a variable with 73 elements
The ans to that command is 0, so I'm realizing that it is not overwriting anything.
Before that, I've read dd by using dd=fread(fp,73,'int16'); and it works perfectly.
Can someone help me please? Thanks in advanced,
RRR
  4 Kommentare
Ameer Hamza
Ameer Hamza am 3 Nov. 2020
Can you paste your code here exactly.
Roselena Rubino
Roselena Rubino am 3 Nov. 2020
fp=fopen(dbldir,'r+');
nmaps=fread(fp,1,'uint32'); %number of maps=13
for ii=1:nmaps
fseek(fp,14,'cof');
nlat=fread(fp,1,'uint16'); %number of latitudes=71
for jj=1:nlat
fseek(fp,20,'cof');
nlon=fread(fp,1,'int16'); %number of longitudes=73
Vm=iono_m(jj,1:nlon,ii);
fwrite(fp,Vm(:),'int16'); %overwriting
% fread(fp,nlon,'int16') %This is how the data is read and it works perfectly. I get 73 values
% % I try also this but it doesn't work
% for oo=1:nlon
% fwrite(fp,iono_m(jj,oo,ii),'int16'); %overwriting the VTEC
% end
end
fclose(fp);
My problem is when using fwrite

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 3 Nov. 2020
You must fseek every time you switch between reading and writing. Telling fseek to move 0 bytes cof is fine for this purpose.
  1 Kommentar
Roselena Rubino
Roselena Rubino am 11 Nov. 2020
Thank you VERY MUCH. Problem solved. That was the issue.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by