필터 지우기
필터 지우기

how can I count/read an Enter as one character?

조회 수: 1 (최근 30일)
Jaybee
Jaybee 2014년 3월 9일
댓글: Image Analyst 2014년 3월 10일
so here's the problem, i have a text file named sampletext.txt containing:
Hello Word
1 2 3 4 5 6
,I used the code
fid = fopen('sampletext.txt');
A = char(fread(fid,inf)).';
L = length(A)
the expected output is 22 but is outputs 23, it counts enter as 2 character. please help. :)
thankyou!

채택된 답변

Jan
Jan 2014년 3월 9일
The character you call "enter" can be char([13, 10]) for DOS formatted ASCII text. Then counting this as 2 characters is correct. Why do you think this is wrong?
But could open the file under Windows in text-mode:
fid = fopen('sampletext.txt', 'rt');
Then the line reak is converted to a char(10) implicitely. But this has the disadvantage, that is does not apply the same conversion under Linux. Writig platform dependent code is a bad programming practize, so I'd rely on counting two bytes as two characters.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 3월 9일
Why not simply use fgetl() if you want to read line by line?
  댓글 수: 2
Jan
Jan 2014년 3월 9일
@Image Analyst: Jaybee did not say, that the file should be read line by line.
Image Analyst
Image Analyst 2014년 3월 10일
No but I just offered as a possibility in case he wants to and to not have to worry about 10s and 13s.

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

카테고리

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