Recent

Author Topic: Recording text file in ansi format  (Read 2886 times)

iginfo

  • New Member
  • *
  • Posts: 18
Recording text file in ansi format
« on: October 23, 2018, 04:17:35 pm »
I need to write data coming from the database with accented characters in utf8 to a text file in ansi format using the commands assign, rewrite and writeln. But the text file is always written in utf8 format.

Any guidance?

Lazarus 1.8.4 FPC 3.0.4 windows 10.


Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Recording text file in ansi format
« Reply #1 on: October 23, 2018, 04:35:38 pm »
Try SetTextCodePage like in this utility function:

Code: Pascal  [Select][+][-]
  1. procedure SaveAsText(const FileName:string;Text:TStrings;const LineEnding:TLineEndStr=sLineBreak;const TextCodePage:TSystemCodePage=CP_NONE);
  2. var
  3.   i:integer;
  4.   f:system.Text;
  5.  
  6. begin
  7.   AssignFile(f,Filename);
  8.   if TextCodePage<>CP_NONE then
  9.     SetTextCodePage(f,TextCodePage);
  10.   SetTextLineEnding(f,LineEnding);
  11.   try
  12.     Rewrite(f);
  13.     for i:=0 to Text.Count-1 do
  14.       WriteLn(f,Text[i]);
  15.   finally
  16.     system.Close(f);
  17.   end;
  18. end;
  19.  


iginfo

  • New Member
  • *
  • Posts: 18
Re: Recording text file in ansi format
« Reply #2 on: October 23, 2018, 07:11:55 pm »
The function with CP_ASCII parameter removed the accents from the text, however generated the file as UTF8, when desired was ANSI.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Recording text file in ansi format
« Reply #3 on: October 23, 2018, 07:58:52 pm »
One other thing you can do is convert each string before writeln'ing it:
Code: Pascal  [Select][+][-]
  1. procedure SaveAsText(const FileName:string;Text:TStrings;const LineEnding:TLineEndStr=sLineBreak;const TextCodePage:TSystemCodePage=CP_NONE);
  2. var
  3.   i:integer;
  4.   f:system.Text;
  5.   sv: AnsiString;
  6. begin
  7.   AssignFile(f,Filename);
  8.   if TextCodePage <> CP_NONE then begin
  9.     SetTextCodePage(f,TextCodePage);
  10.     SetCodePage(sv, TextCodePage);
  11.   end else
  12.     SetCodePage(sv, CP_UTF8);
  13.   SetTextLineEnding(f,LineEnding);
  14.   try
  15.     Rewrite(f);
  16.     for i:=0 to Text.Count-1 do begin
  17.       sv := Text[i]; {FPC will convert the string for you}
  18.       WriteLn(f, sv);
  19.     end;
  20.   finally
  21.     system.Close(f);
  22.   end;
  23. end;
  24.  
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Recording text file in ansi format
« Reply #4 on: October 23, 2018, 08:27:53 pm »
The function with CP_ASCII parameter removed the accents from the text, however generated the file as UTF8, when desired was ANSI.

That's because ansi doesn't support accents. Select the correct codepage that HAS accents to output them.

Lutz Mändle

  • Jr. Member
  • **
  • Posts: 65
Re: Recording text file in ansi format
« Reply #5 on: October 23, 2018, 09:58:37 pm »
The following link gives an overview about the usable code page identifiers:

https://docs.microsoft.com/en-us/windows/desktop/intl/code-page-identifiers

I'm using the given function with the following identifiers: 437, 1252 and 28591.


iginfo

  • New Member
  • *
  • Posts: 18
Re: Recording text file in ansi format
« Reply #6 on: October 23, 2018, 10:05:22 pm »
Lutz Mändle, now is working with codepage 1252.

Thank you and all who helped!

 

TinyPortal © 2005-2018