Matlab quickie: test if text file is empty

A simple question: I open the file in matlab 7.x and I want to check if it is empty before reading. What is the best way to do this?

+4
source share
3 answers

Using some knowledge from this previous question , I would do the following

s = dir('c:\somefile.txt'); if s.bytes == 0 % empty file else % open the file and read it end; 

I assumed empty that you meant that there really was nothing in the file, including new string characters. If by blank you mean only new string characters, then you should continue your decision.

+9
source

got:

 fid = fopen(fil); if all(fgetl(fid) == -1) % file is empty else fseek(fid,0,-1); % rewind it end 
0
source

This is the cleanest way I can think of:

 if fseek(fileID, 1, 'bof') == -1 % empty file else rewind(fileID) % ready to read end 
0
source

Source: https://habr.com/ru/post/1302430/


All Articles