필터 지우기
필터 지우기

fgets write -1.000000e+00 in output file

조회 수: 1 (최근 30일)
Mohamed AKI Ahmed
Mohamed AKI Ahmed 2022년 3월 21일
댓글: Mohamed AKI Ahmed 2022년 5월 30일
Hello,
I have a code that first opens a file (using fopen), then gets everyline from the file, however, when encountering the end-of-file, and the value is -1, it prints this value in the file generated. Thus, when comparing the original file with the one generated, I have the value -1.000000e+00 written at the end. I want to exclude this value, any suggestions?
file=fopen('poem.txt');
line_ex=fgets(file);
getit{1}=filepath %filepath was indicated before
getit{end+1}=line_ex
while ischar(line_ex)
line_ex=fgets{file};
getit{end+1}=line_ex
end
fclose(file);

채택된 답변

Stephen23
Stephen23 2022년 3월 21일
while ~feof(file)
  댓글 수: 3
Stephen23
Stephen23 2022년 3월 22일
It works for me:
fid = fopen('old.txt','rt');
fnd = fopen('new.txt','wt');
while ~feof(fid)
str = fgetl(fid);
fprintf(fnd,'%s\n',str);
end
fclose(fid);
fclose(fnd);
type new.txt
AA BB CC
Clearly you are doing something different. But because you do not show your code, I cannot debug it.
Mohamed AKI Ahmed
Mohamed AKI Ahmed 2022년 5월 30일
Sorry for the late reply. It does work now, thanks a lot for your help

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

추가 답변 (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