Writing a integer in an existing file

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

Ameer Hamza
Ameer Hamza 2020년 11월 3일
This command is working fine on my system. Have you checked the content of the file using hexdump?
Roselena Rubino
Roselena Rubino 2020년 11월 3일
fwrite(fp,dd,'int16'); is not able to write. I know it because its output is 0 showing that it didn't write any number in the file. :(
Ameer Hamza
Ameer Hamza 2020년 11월 3일
Can you paste your code here exactly.
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

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 11월 3일

0 개 추천

You must fseek every time you switch between reading and writing. Telling fseek to move 0 bytes cof is fine for this purpose.

댓글 수: 1

Roselena Rubino
Roselena Rubino 2020년 11월 11일
Thank you VERY MUCH. Problem solved. That was the issue.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2020년 11월 3일

댓글:

2020년 11월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by