Fid=-1 for fopen

10 次查看(过去 30 天)
ck
ck 2016-6-22
评论: ck 2016-6-22
So I am trying to open a bunch of .dat files(ex:frm_ais_edr_nnnn.dat', where n varys), so when I put it in a loop , fopen works for first time and when the loop runs for the second time and it takes next .dat fil fopen returns fid=-1 and it cannot be processed. the Code is as below:
clear;
clc;
filepath='C:\Users\K chetan\Desktop\marsis_DAta\datafiles';
out=dir(filepath);
n = length(out);
for count=3:n
out(count,1).name
fid=fopen(out(count,1).name,'r');
data_file= fread(fid,'*char');
fclose(fid);
end
here the file path is the directory in which all files are present.
How can I overcome this error ?
the error its showing is
count =
4
fid =
-1
Error using fread
Invalid file identifier. Use
fopen to generate a valid file
identifier.
Error in directory_test (line 8)
data_file= fread(fid,'*char');
thank you

采纳的回答

Walter Roberson
Walter Roberson 2016-6-22
fid = fopen( fullfile(filepath, out(count,1).name), 'r');
  2 个评论
ck
ck 2016-6-22
will I need to put this in a loop?
ck
ck 2016-6-22
YEAH ! it worked , thanks a ton!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by