fopen in text mode ('rt') giving wierd results

조회 수: 15 (최근 30일)
Matt
Matt 2014년 10월 7일
답변: Matt 2015년 5월 13일
I am currently using version 2013a on Windows but I have received a project from our old Linux server. There is a line in it which opens a binary file like this:
fp = fopen(file, 'rt', 'l');
data = fread(fp, inf, 'float32');
When I use this command on Linux I get the expected data out of the file. When I use the same command on Windows I get one fewer number and everything after the nth entry is bad until the mth entry. This is very repeatable for a given file. Does the text mode force Matlab to look for ascii new lines even in a binary file? If I change the fopen command on Windows to this:
fp = fopen(file, 'r', 'l');
The problem goes away. I am hoping this is just something that this is just a bug with our old code that was never tested on Windows.
  댓글 수: 2
Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014년 10월 8일
As you pointed out, the 'rt' permission argument opens the file for reading in text mode. On Windows machines, the carriage return character \r is removed when reading text in.
Is the file binary? If yes, does the file contain only numbers? If yes, what number is missing?
13 is the ASCII code for carriage return. I would assume that opening the file for reading in text mode mistakes the number 13 for a carriage return, or that one of the number you read in contains the byte 13 (out of the four bytes a float is made of).
I believe that 't' has no effect on UNIX machines, so the question is: Why are you opening the file in text mode?

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

채택된 답변

Matt
Matt 2015년 5월 13일
As Bruno Pop-Stefanov pointed out the problem is opening a binary file in text mode on Windows. The behavior seen was expected given the contents of the file. The solution of opening binary files without the 't' is correct.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by