Strange fscanf function behavior

I am trying to read the information contained in a small configuration file with the Matlab fscanf function. File contents:

YAcex: 1.000000
YOx: 1.000000
KAce: 1.000000

Matlab code used to analyze the file:

fh = fopen('parameters', 'r');
fscanf(fh, 'YAcex: %f\n')
fscanf(fh, 'YOx: %f\n')
fscanf(fh, 'KAce: %f\n')
fclose(fh);

When this script is called, only the string "YAcex" is read correctly; fscanf returns []for two other lines. If the lines YOx and KAce are switched (KAce to YOx), all lines are read correctly by fscanf.

Can anyone explain this behavior?

additional information: Linear roots in the input file is a simple line feed (\ n character, without \ r character).

+4
source share
2 answers

, fscanf, . :

fscanf . fscanf formatSpec , , .

, , , . , Y of YOx: YAcex: . Y YOx:, fscanf , Ox: .... ftell:

fh = fopen('parameters', 'r');
fscanf(fh, 'YAcex: %f\n');
ftell(fh)

ans =

    18    % The "O" is the 18th character in the file

YOx: KAce: , , .

, ? , , fscanf :

fh = fopen('parameters', 'r');
fscanf(fh, 'YAcex: %f\n', 1);
fscanf(fh, 'YOx: %f\n', 1);
fscanf(fh, 'KAce: %f\n', 1);
fclose(fh);

- :

fh = fopen('parameters', 'r');
values = fscanf(fh, 'YAcex: %f\n YOx: %f\n KAce: %f\n');
fclose(fh);

values 3 1, 3 .

+8

, \r \r\n . , , , , -. , uint8 , :

u8 = fread(fh, inf, '*uint8')';

- char :

fh = fopen('parameters');
ch = fread(fh, inf, '*char')'; % read all as char
fclose(fh);

YAcex = regexp(ch, '(?<=YAcex:\s?)[\d\.]+', 'match', 'once'); % parse YAcex 

. , -, .

+1

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


All Articles