필터 지우기
필터 지우기

how can we check file existence?

조회 수: 21 (최근 30일)
Safiullah Khan
Safiullah Khan 2011년 4월 12일
hi, i am writing my application(gui app) data to a text file (appdata.txt) and i want to make sure that every time when the application runs it doesn't create a new file or a copy of a file, the problem is:
1) how can i check if the appdata.txt already exists or not (if not then create new text file or else append data to existing one)
thanks for any help.

채택된 답변

Walter Roberson
Walter Roberson 2011년 4월 12일
if exist('appdata.txt', 'file')
However, the bit about appending suggests that all you really need to do is,
fid = fopen('appdata.txt', 'at');
This will create a new file if needed and append to an existing file if it is there.
  댓글 수: 1
Jan
Jan 2011년 4월 12일
EXIST(Name, 'file') replies a non-zero number for directories also:
if exist(tempdir, 'file'), disp('found'); end
A better check is:
if exist(Name, 'file') && ~exist(Name, 'dir')
or:
if exist(Name, 'file') == 2
I prefer "fopen(Name, 'r+')", because the append mode needs to locate the end of the file at first, which is time-consuming for large files.

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

추가 답변 (2개)

Safiullah Khan
Safiullah Khan 2011년 4월 12일
thank you so much Walter Roberson for your reply. it did worked for me.

Yahia chs
Yahia chs 2011년 5월 20일
thank you very much this was really felpful!!!!!!!

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by