Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import a text file in ANSI format 1

Status
Not open for further replies.

doctorjellybean

Programmer
May 5, 2003
145
0
0
I use a TMemo component to display the contents of imported text files. Some of the files are not ANSI formatted, although they display perfectly in Notepad. These files display a weird character in TMemo.

71vuayu.jpg


I suspect it is opened in Unicode format, because if I select Save As in Notepad, the Unicode option is displayed.

7wq1fur.jpg


Is there a way to import/open it in ANSI format?

Thank you.
 
Yes, but you'll have to do the conversion manually.

The following assumes two things.
1. The file is Unicode and not some variable-width encoding scheme.
2. Non-ANSI characters can be converted to '?'.

No error is reported if the function fails.

Code:
procedure LoadUnicodeIntoMemo( memo: tMemo; filename: string );
  var
    filestream: tFileStream;
    filetext:   widestring;
  begin
  try
    filestream := tFileStream.create( filename, fmOpenRead );
    try
      SetLength( filetext, filestream.size div 2 );
      filestream.read( filetext, filestream.size and not 1 );
      memo.text = string( filetext )
    finally filestream.free end
  except end
  end;

Hope this helps.
 
Thanks Duoas [smile].

Could you possibly tell me how to use this? I just had a go and have to admit I'm at a loss.

Thank once again.
 
Apologies if I appear to be lazy, etc. Theory has never been my strong point, even at school. The practical is more my forte, I can try things that way until I figure out how it all works. The same with coding, just reading it scrambles my brain. Seeing it in working/demo mode, I can try things out and learn from that [smile].
 
Heh, don't feel bad. There are different ways to learn things. After a while you'll be able to see things from other perspectives more easily, but until then don't apologize.

Here's an example. The function I gave you above should be somewhere at the beginning of the implementation section of your unit.

I presume you are loading the memo from a button click procedure. The following example presumes the button is named 'Button1', so just replace all 'Button1's in the example to whatever the actual name is. Likewise for the form: I presume 'Form1'. And likewise for your open dialog, etc.
Code:
procedure Form1.Button1Click( Sender: TObject );
  begin
  with OpenDialog1 do
    if Execute 
      then LoadUnicodeIntoMemo( Memo1, FileName )
  end;

And that's it!

Hope this helps.
 
Argh. I wish this forum let you edit your posts. I just noticed a typo in the 'LoadUnicodeIntoMemo' procedure I gave you.

Down at the bottom it should be
Code:
memo.text := string( filetext )

(I use C++ a lot also... sorry :-S )
 
Down at the bottom it should be

memo.text := string( filetext )

lol, I figured that out when I tried it the first time. Interesting that C++ allows memo.text = string( filetext ).

Yes, allowing editing of posts would make things a lot easier [smile]
 
Right, the code works (D2007). However, when I open a text file, the application exits. The debugger complains of an access violation, but doesn't actually highlight the problematic code.

This is the easy way, but I have found a 3rd party memo component which actually displays the text after that strange character. I guess TMemo is not foolproof.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top