2
procedure p;
var 
  f:TextFile;
  oemst:OemString;///declared as   OemString = Type AnsiString(CP_OEMCP);
begin
  AssignFile(f,fileName);
  reset(f);
  read(f,oemSt);
  ShowMessage(oemst);
end;

In order to see within the show message the oemst in the proper encoding, one needs to declare: setcodepage(oemst,862,false);

How is it possible to read from an oem file, and make sure the compiler show it correctly in oem encoding?

update: Working with Oem is a MUst, as I don't specify this old interface. working with readln assign file is part of the old code, and I rather have a solution using the old methods as it requires less work and testing.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
none
  • 4,669
  • 14
  • 62
  • 102

2 Answers2

2

In D2009 and up you are usually better off not using the old Pascal file methods like assignfile, read(ln) and write(ln) as they do not support Unicode. And everybody will advice you to use streams instead. In this case you may actually be better of using the old way because it doesn't understand Unicode.

While you are setting the code page correctly, what I think you need to do to get this to work for you, is set the console's code page using SetConsoleCP. That is a Windows API call which is declared in the Windows unit.

Marjan Venema
  • 19,136
  • 6
  • 65
  • 79
  • Can you add screenshot of the ShowMessage and of an original correct display of the characters in the file that are displayed incorrectly by ShowMessage? With regard to this, nothing beats a picture. The hex content of the file for the parts that are displayed incorrectly by ShowMessage would be helpful as well. – Marjan Venema May 19 '11 at 09:43
  • 1
    Ok, thanks. I assume your system font is capable of displaying the correct characters and that the default code page of your machine is in fact set to 862? Could you add a screenshot of the correct text so I can see whether I am getting something that is even remotely correct? At the moment I have no idea what the text in your ShowMessage should like... – Marjan Venema May 19 '11 at 11:00
2

SetMultiByteConversionCodePage(862); moved things along.

none
  • 4,669
  • 14
  • 62
  • 102
  • 1
    Nice and simple. Great and thanks for letting us know. If "moved things along" means it solved your problem, please accept your own answer... – Marjan Venema May 19 '11 at 12:54
  • so the problem here is how to restore the system byte conversion code page.... bah. – none May 22 '11 at 09:59
  • 1
    Well, if you look at what SetMultiByteConversionCodePage does, it becomes obvious that you only need to read the value of DefaultSystemCodePage to retrieve the current value; then use SetMultiByteConversionCodePage to set it to the one you want; and after you are done use SetMultiByteConversionCodePage again to set it the value you retrieved. (Remember to use a try finally to ensure it gets reset.) – Marjan Venema May 22 '11 at 10:43