Recent

Author Topic: read text file position base  (Read 959 times)

Packs

  • Sr. Member
  • ****
  • Posts: 479
read text file position base
« on: February 12, 2025, 01:09:39 pm »
Dear Team,

I am getting text file with data. these data are in position base .

I know I have to loop the line and cut each line position base .

I have used same think in ado.net where we have to pass position and it create dataset for me .

is there any best method or library .

Please guid me

cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: read text file position base
« Reply #1 on: February 12, 2025, 07:43:17 pm »
Hi
Too little information...
You can use 'Copy()' to get substrings position-wise:
Param1 = source string full
Param2 = start-position (1 based)
Param3 = length of the substring you wish to extract / copy
Example:
Code: Pascal  [Select][+][-]
  1. var
  2.   ls: string = 'The quick brown fox, jumped over the lazy dog';
  3.   sub: string;
  4. begin
  5.   sub:= copy(ls,1,20);
  6.   writeln('copy 1..20 = ',sub); { prints "The quick brown fox," }
  7.   sub:= copy(ls,21,6);
  8.   writeln('copy 21..27 = ',sub); { prints "jumped" }
  9. // etc.
  10. end;
  11.  
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Bart

  • Hero Member
  • *****
  • Posts: 5538
    • Bart en Mariska's Webstek
Re: read text file position base
« Reply #2 on: February 12, 2025, 10:27:58 pm »
Can you give an example of a text file, and what data you need to extract from it?

Bart

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: read text file position base
« Reply #3 on: February 13, 2025, 02:31:39 am »
As mentioned, we need the an example of the text file.

I have written 2 text file demos, you can download them from here:
https://wiki.freepascal.org/Portal:HowTo_Demos#File_handling
« Last Edit: February 13, 2025, 02:34:20 am by Handoko »

Packs

  • Sr. Member
  • ****
  • Posts: 479
Re: read text file position base
« Reply #4 on: February 14, 2025, 07:20:40 am »
Code: Pascal  [Select][+][-]
  1.  
  2.  inward_data =  TFixedFormatDataSet
  3.  
  4. this is file data
  5. 800015023800068002301120240000003000000027974810100022013                       FTT002027974291120240107658101000220.TIFBTT002027974291120240107658101000220.TIFFTJ002027974291120240107658101000220.TIF291120246000150000107658101000220       XXXXXXX X XXXXX                006180600000010
  6.  
  7.  
  8.     inward_data.Close;
  9.  
  10.     inward_data.Schema.Append('presenting_bank=9');
  11.     inward_data.Schema.Append('drawee_bank=9');
  12.     inward_data.Schema.Append('bussiness_date=8');
  13.     inward_data.Schema.Append('amount=13');
  14.     inward_data.Schema.Append('inst_no=6');
  15.     inward_data.Schema.Append('item_seq_no=10');
  16.     inward_data.Schema.Append('tran_code=2');
  17.     inward_data.Schema.Append('blank=23');
  18.     inward_data.Schema.Append('image=120');
  19.     inward_data.Schema.Append('udk=34');
  20.     inward_data.Schema.Append('particular=37');
  21.     inward_data.Schema.Append('account=15');
  22.     inward_data.Schema.Append('blank_space=1');
  23.  
  24.     inward_data.FileName:=Opendlg_inward.FileName ;
  25.     inward_data.Open;
  26.  
  27.  

it is showing wrong output . I have attached the error file too


Packs

  • Sr. Member
  • ****
  • Posts: 479
[SOLVED]Re: read text file position base
« Reply #5 on: February 14, 2025, 08:30:59 am »
it is solved

    inward_data.CodePage := 'windows-1252'; //utf-8

cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: read text file position base
« Reply #6 on: February 14, 2025, 11:24:23 am »
Hi
Sorry mate, but these 2 are NOT AT ALL THE SAME!
Quote
inward_data.CodePage := 'windows-1252'; //utf-8
CP-1252 is a single-byte codepage, like CP-8859_1
Whereas UTF8 is a multi-byte codepage, i.e.: 1 codepoint = 1..4 bytes
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Packs

  • Sr. Member
  • ****
  • Posts: 479
Re: read text file position base
« Reply #7 on: February 14, 2025, 11:32:34 am »
I have used following method it is working for me
 inward_data.CodePage := 'windows-1252';


cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: read text file position base
« Reply #8 on: February 14, 2025, 11:36:32 am »
Hi
By the looks of your data, that should be correct, at a quick glance, they seem to be single-byte.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Packs

  • Sr. Member
  • ****
  • Posts: 479
Re: read text file position base
« Reply #9 on: February 14, 2025, 11:41:51 am »
if you don't mind can you explain me in detailed

what is codepage . why it is needed

it should be simple position base parsing .

cdbc

  • Hero Member
  • *****
  • Posts: 1964
    • http://www.cdbc.dk
Re: read text file position base
« Reply #10 on: February 14, 2025, 11:59:58 am »
Hi
A codepage is simply a table of characters that can be represented by the 0..255 byte values of one single byte ~ /old-school/
In the DOS-days we used e.g.: CP850 a lot, then came windows and we used CP-1252 a lot, then windows switched to 'mbcs' ~ min 2 bytes per char/codepoint ~ UTF16 ~ WideString. Nowadays windows too supports UTF8.
This codepage / unicode stuff is a huge technical field, with lots of research and work going into it.
example:
Code: Text  [Select][+][-]
  1. Text: Peña
  2.  
  3. text    sgl-byte    multi-byte
  4. P       1           1
  5. e       1           1
  6. ñ       1 #nnn      2 #C3nn
  7. a       1           1
  8. ------------------------------
  9. 4       4           5
In multi-byte, all the ascii chars (0..127) are the same as in single-byte.
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Packs

  • Sr. Member
  • ****
  • Posts: 479
Re: read text file position base
« Reply #11 on: February 14, 2025, 12:07:45 pm »
Thank you so much for detailed expiation 🙏🙏🙏🙏

 

TinyPortal © 2005-2018