How to implement? If you try to read using the read function, you get the error "invalid numeric input".

Sample file:

1254,1 1345,88 24,24 

Code example:

 procedure example(); var f: TextFile ; i: integer; j:double; begin AssignFile(f, 'text.txt'); Reset(f); for i := 1 to 3 do read(f,j); end; 
  • Please add a reading code and sample file format to the question. - kami
  • one
    Try using a comma instead of a comma, if it helps, then change the locale in the program. - pavel

1 answer 1

Your code contradicts itself.

You use a text file, and try to read a number from it. Read the string and convert it to a number via StrToFloat , for example:

 var FS: TFormatSettings; s: string; GetLocaleFormatSettings(GetThreadLocale, FS); FS.DecimalSeparator:=','; read(f, s); j := StrToFloatDef(s, 0, FS); 

PS It is highly desirable to avoid using the functions of AssignFile / Append / Read / Write in favor of more correct TStringList , TStringStream , etc classes ...

  • According to your code, the entire string is written to s (before the end-of-line character, not a space, I mean), and j = 0, I need to count all the numbers sequentially - Dec
  • @Dec. There is an error in the text of my answer (something a bit too much for today) - the code does not contradict (I forgot how read / readln works), but for the correct work in the file there should be a separator for the whole and fractional parts . . Direct installation of DecimalSeparator does not give effect. If you change the original file format is not possible - you need to separate the data yourself, using space as a separator. - kami